util.defineClass

Static function.

Base function implementing class declaration in the Yandex Maps API. Use this function to declare a new class, specify a set of methods for it and inherit from another class.

The "superclass" field is appended to the child class, specifying the prototype of the parent class. Use the 'constructor' field of the 'superclass' object to refer to the constructor of the parent class.

Returns class.

{ Object } util.defineClass(constructor[, parentClass[, override]])

Parameters:

Parameter

Default value

Description

constructor*

Type: Function

Class constructor.

parentClass

Type: Function

Parent class to inherit from. This argument may be omitted.

override

Type: Object

Set of additional fields and functions that will be appended to the class prototype. There can be several sources (the function can have any number of parameters), and data is copied from right to left (the last argument has highest priority during copying).

* Mandatory parameter/option.

Examples:

1.

// Declaring a class with methods.
var MyClass = ymaps.util.defineClass(function () {
    this.field = 'fieldValue';
}, {
    doSomethingAwesome: function () {
        return 'methodResult';
    },
    stop: function () {
        //...
    }
});
var object = new MyClass();
console.log(object.field); // 'fieldValue'
console.log(object.doSomethingAwesome()); // 'methodResult'

2.

// Creating a custom class for a button that is inherited from the base class of the button control.
// Clicking the button switches the type of map tiles.
var CustomControl = ymaps.util.defineClass(function () {
    // Defining a limited set of options that can't be changed from outside.
    var data = {
        data: { content: 'Change the map type' },
        options: {
            selectOnClick: false,
            maxWidth: 150
        }
    };
    CustomControl.superclass.constructor.call(this, data);
}, ymaps.control.Button, {
    // Setting a list of class methods.

    // Overriding the button enable and disable methods.
    enable: function () {
        // You must call base class methods
        // to avoid breaking the button logic.
        CustomControl.superclass.enable.call(this);
        // Enabling and disabling button behavior.
        this.events.add('click', this.switchType, this);
    },

    disable: function () {
        this.events.remove('click', this.switchType, this);
        CustomControl.superclass.disable.call(this);
    },

    // Implementing our own methods.
    switchType: function () {
        var map = this.getMap();
        if (map) {
            if (map.getType() == 'yandex#map') {
                this.setSatelliteMapType();
            } else {
                this.setSchemeMapType();
            }
        }
    },

    setSchemeMapType: function () {
        var map = this.getMap();
        if (map) {
            map.setType('yandex#map');
        }
    },

    setSatelliteMapType: function () {
        var map = this.getMap();
        if (map) {
            map.setType('yandex#satellite');
        }
    }
});
// Instantiating a new class and adding it to the map.
var typeSwitcherButton = new CustomControl();
// The map creation code was omitted in this example.
myMap.controls.add(typeSwitcherButton);
// Calling the method of the class instance.
typeSwitcherButton.setSatelliteMapType();