ООО «ПРкениг»
236006 Калининград Московский проспект 40
8 (4012) 39 09 55
info@pr-kenig.ru,

Constructors in JavaScript objects (js)

Constructors in JavaScript objects (js)
Constructors in JavaScript objects (js)
Author Dmitry Grozny Dmitry Grozny 14/08/2018

Techniques I use when creating constructors in JavaScript objects. Inheritance.

Parent dynamic object

var BaseObject = function (arg1) {
    var t = this;
    // private
    var number = 1;
    // public
    this.text = 'text';
    // public method
    this.myFunctionPublic = function () {
        console.log('myFunctionPublic BaseObject');
        return t.text;
    };
    // private method
    var myFunctionPrivate = function () {
        console.log('myFunctionPrivate');
    };
    // constructor
    this.constructor = (function () {
        myFunctionPrivate();
        t.myFunctionPublic();
        console.log('number', number);
        console.log('text', t.text);
        console.log('this', t);
        console.log('arg1', arg1);
    })();
};


Parent created dynamic object

var CreatedBaseObjectDynamic = (function () {
    // private property
    var number = 1;
    // private method
    var privateMethod = function () {
        console.log('privateMethod');
    };
    // constructor
    var object = function () {
        //base object public property
        this.name = 'name';
        //base object public method
        this.myFunctionPublic = function () {
            console.log('myFunctionPublic');
        };
        //run constructor method
        this.constructor.constructorMethod();
    };
    //constructor public property
    object.constructor_property = 'constructor_property';
    //constructor public method
    object.constructorMethod = function () {
        return number;
    };
    //add base object public property
    object.prototype.add_object_property = 'add_object_property';
    //add base object public method
    object.prototype.myFunctionPublic2 = function () {
        console.log('myFunctionPublic2');
    };
    //run constructor
    return object;
})();


Parent static object

var BaseObjectStatic = {
    // public
    text: 'text',
    get title() { return 'title';},
    // public
    myFunctionPublic: function () {
        console.log('myFunctionPublic BaseObjectStatic');
    }
};

Child obejct extends parent

var MyObject = function () {
    // extends dynamic object
    this.__proto__ = new BaseObject('argument 1');
    // OR extends static object
    this.__proto__ = BaseObjectStatic;
    // OR extends created dynamic object
    this.__proto__ = new CreatedBaseObjectDynamic();
    // public property
    this.text = 'new text';
    // public method
    this.myFunctionPublic = function () {
        this.__proto__.myFunctionPublic();
        console.log('myFunctionPublic MyObject');
        console.log('text;', this.text);
    };
};


Created instance

var instance = new MyObject();
instance.myFunctionPublic();

js

Views: 392

Order the project

Our team of professionals is ready to implement your projects, fill out the form: