Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript how to override a constructor method?

Tags:

javascript

oop

I have a View class that has a constructor method initialize I think this is a constructor method, correct me if wrong.

var View = function(obj) {
    this.initalize = obj.initalize;
};

What I would like to achieve is something gets called when the Class is instantiated.

How can I pass in an object like so?

var chatView = new View({
    initialize: function() {
        alert('Yay for initialization');
    }
});

So that when I instantiate the View I can pass an object to the constructor and within a key initialize which value is a function and this key specifically gets called when instantiated.

like image 428
Michael Joseph Aubry Avatar asked Sep 16 '25 11:09

Michael Joseph Aubry


2 Answers

If I get it right, there is a simple way of achieving what you want:

var View = function(obj) {
    obj.initialize();
}

This way, the initialize function gets called whenever you instantiate a View class.

Be aware that if you want to do real "initialization code" inside the initialize function to work, you could use call (or apply):

var View = function(obj) {
    if (typeof obj.initialize === 'function') {
        obj.initialize.call(this);
    }
}

var chatView = new View({
    initialize: function() {
        this.property = 'property';
    }
});

console.log(chatView.property); // outputs property
like image 197
Samuel Avatar answered Sep 18 '25 23:09

Samuel


Javascript doesn't have a constructor, remember that javascript is based on prototype. This is an example of "constructor" you can create

function Example (firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}

Example.prototype.getFullname = function () {
return this.firstname + ' ' + this.lastname;
}

If you want to create a constructor function you must to call it after you instantiate the function.

But this is a better structure you can use. I recommended only if you need a constructor function and private function. Otherwise, use a simple structure with methods declared with prototype, you can get a better performance.

var MyObject = (function () {

    // Constructor
    function MyObject (foo) {
        this._foo = foo;
    }

    function privateFun (prefix) {
        return prefix + this._foo;
    }

    MyObject.prototype.publicFun = function () {
        return privateFun.call(this, '>>');
    }

    return MyObject;
})();


var myObject = new MyObject('bar');

With this code you have a constructor, but it's a "private" function, so you can't overwrite it after instantiate the object.

Here I have a link I create testing differents structures: https://plnkr.co/edit/qzgWVZlnIFnWl0MoUe5n?p=preview

The result:

Test 1: ~15k (with private function) - Recommended ONLY if you want/need a private function

Test 2: ~38k (with private function) - Not recommended, it's returning an object which is really bad.

Test 3: ~8k (without private function) - Recommended, it has the best performance, but you can't create a private function, which means, anybody can call any function :S

like image 25
Giuliano Avatar answered Sep 19 '25 01:09

Giuliano