简体   繁体   English

扩展init方法而无需递归,javascript

[英]extending init method without being recursive, javascript

I'm trying to extend functionality from using prototypes, but keep getting Maximum call stack size exceeded, basically, I'm doing this: 我正在尝试通过使用原型来扩展功能,但是一直保持超出最大调用堆栈大小的原则,我正在这样做:

 var Parent = function() {};
 Parent.prototype.init = function(options){
    this.val_one = options.one;
    /*more init code*/
 };

var Child = function() {
   Parent.call(this);
};
Child.prototype = Parent.prototype;

Child.prototype.init = function(options) {
    this.val_two = options.two;
    //I also need val_one and all the initiation placed on the parent while keeping the init method name, (shadowing while extending?)

   Parent.prototype.init.call(this, options); 
   //will result in a self loop
   //copy all the method for adding a single extra value? is this the only way to do achieve this?
};

Any help will be appreciated. 任何帮助将不胜感激。

 Child.prototype = Parent.prototype; 

is your problem. 是你的问题。 You must not use that for "inheritance" - it makes Child and Parent instances have exactly the same prototype object. 不得将其用于“继承”-这会使ChildParent实例具有完全相同的原型对象。

Morever, as you can test, Parent.prototype.init === Child.prototype.init - you've overwritten the original init method (the one you created using Parent.prototype.init = function(options){…} ) on that single object in the assignment Child.prototype.init = function(options) {…} . 此外,可以测试的是Parent.prototype.init === Child.prototype.init您已经覆盖了原始的init方法(使用Parent.prototype.init = function(options){…}创建的方法)分配Child.prototype.init = function(options) {…} 中的单个对象 So your "super" call is actually calling the method itself again, which leads to your unbound recursion. 因此,您的“超级”调用实际上是在再次调用方法本身,这导致您进行未绑定的递归。

Instead, use 相反,使用

Child.prototype = Object.create(Parent.prototype);

and it will work as expected, as those are two distinct objects now. 它将按预期工作,因为现在这是两个不同的对象。

The init function you made is just another function , it is NOT the constructor and will not be called unless you call it within the constructor. 您创建的init函数只是另一个函数,它不是构造函数,除非您在构造函数中调用它,否则不会调用它。

I made an example for you, hopefully this helps: https://jsfiddle.net/hfmbv98g/ 我为您提供了一个示例,希望对您有所帮助: https : //jsfiddle.net/hfmbv98g/

var Parent = function(options) {
// constructor
this.val_one = options.one;
console.log("parent");
};

Parent.prototype.other_method = function() {
    console.log("Parent: other_method")
}

var Child = function(options) {
   Parent.call(this, options);
   this.val_two = options.two
   console.log("child");
};

Child.prototype = Object.create(Parent.prototype); // See note below
Child.prototype.constructor = Child;


Child.prototype.other_method = function() {
    console.log("Child: other_method")
};

You should also read up on this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript 您还应该阅读以下内容: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript

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

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