javascript - Setting C.prototype.constructor = C leads to StackOverflow -
javascript - Setting C.prototype.constructor = C leads to StackOverflow -
i have been learning js inheritance , study project, i'm creating own implementation of inheritance base of operations class extend
method create new classes. utilize c.prototype.constructor = c
stoyan sefanov says to, leads stack overflow when seek phone call base of operations class constructor.
the problem sec level of inheritance keeps on calling it's own constructor! if remove line, inheritance seems work fine.
jsfiddle sample.
here implementation of base of operations class code.
(function(testframework, undefined) { testframework.class = function() {}; testframework.class.extend = function(ctor) { var base of operations = this; var derived = ctor; //duplicate static property access derived (var property in base) { if (base.hasownproperty(property)) derived[property] = base[property]; } //create lightweight intermediate constructor avoid calling base of operations constructor function lightweightctor() { this.constructor = derived; }; //setup prototype chain lightweightctor.prototype = base.prototype; derived.prototype = new lightweightctor(); derived.prototype._mybase = lightweightctor.prototype; derived.prototype._mybasector = function() { base.prototype.constructor.apply(this, arguments); }; //return derived class homecoming derived; }; testframework.class.prototype.classname = "testframework.class"; })(window.testframework);
i spent bit of time looking @ fiddle. i'm not exclusively intend code do, on line 23, have:
base.prototype.constructor.apply(this, arguments);
which calls line 35..
this._mybasector();
.. calls line 23, calls line 35, etc.
two suggestions (forgive me if aware). utilize debugger statement when debugging - indispensable. , consider library underscore.js if want utilize proven inheritance in production environment.
javascript inheritance prototypal-inheritance
Comments
Post a Comment