简体   繁体   English

Javascript如何覆盖构造函数方法?

[英]Javascript how to override a constructor method?

I have a View class that has a constructor method initialize I think this is a constructor method, correct me if wrong. 我有一个具有构造函数方法initializeView类,我认为这是构造函数方法,如果出错,请更正我。

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

What I would like to achieve is something gets called when the Class is instantiated. 我想实现的是在实例化Class时调用的东西。

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. 这样,当我实例化View我可以将一个对象传递给构造函数,并在一个键内initialize哪个值是一个函数,并且在实例化此键时将特别调用此键。

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. 这样,无论何时实例化View类,都将调用initialize函数。

Be aware that if you want to do real "initialization code" inside the initialize function to work, you could use call (or apply ): 请注意,如果您想在initialize函数中执行真正的“初始化代码”以工作,则可以使用call (或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. Javascript没有构造函数,请记住Javascript是基于原型的。 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 这里有一个我创建测试差异结构的链接: https : //plnkr.co/edit/qzgWVZlnIFnWl0MoUe5n?p=preview

The result: 结果:

Test 1: ~15k (with private function) - Recommended ONLY if you want/need a private function 测试1:〜15k(具有私有功能)-仅当您想要/需要私有功能时才建议使用

Test 2: ~38k (with private function) - Not recommended, it's returning an object which is really bad. 测试2:〜38k(具有私有功能)-不建议,它返回的对象确实很差。

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 测试3:〜8k(不包含私有函数)-推荐,它具有最佳性能,但是您不能创建私有函数,这意味着任何人都可以调用任何函数:S

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM