最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

oop - Javascript how to override a constructor method? - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked Jan 25, 2016 at 2:49 Michael Joseph AubryMichael Joseph Aubry 13.5k16 gold badges77 silver badges141 bronze badges 3
  • 1 Possible duplicate of Why is it impossible to change constructor function from prototype? – AMACB Commented Jan 25, 2016 at 2:53
  • What I would like to achieve is something gets called when the Class is instantiated. Then call it. – user663031 Commented Jan 25, 2016 at 3:22
  • Ideally I can just pass an object, similar to Backbone.js github./jashkenas/backbone/blob/master/backbone.js I know they use _.extend() but the idea is when you instantiate a View it does it on its own. – Michael Joseph Aubry Commented Jan 25, 2016 at 3:28
Add a ment  | 

2 Answers 2

Reset to default 4

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

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 remended 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) - Remended ONLY if you want/need a private function

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

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

发布评论

评论列表(0)

  1. 暂无评论