javascript - Finding the constructor in multi-level inheritance -
i want find out specific constructor used instantiate object in javascript, not 1 last in prototype chain. consider code :
function f(){}; function e(){}; function d(){}; function c(){}; function b(){}; function a(){};  e.prototype= new f(); d.prototype= new e(); c.prototype= new d(); b.prototype= new c(); a.prototype= new b();  a=new a();   find fiddle here
a.constructor returns function f(){}, want method returns function a(){}, since a constructor used instantiate object.
how can achieved ?
with way inherit parent it's not possible access original constructor, because when write
a.prototype = new b();   a.prototype.constructor indeed points b not a anymore.
with pattern prototypical inheritance have manually set constructor properly. either each extended class manually or can use helper function:
function inherit(c, p) {      c.prototype = new p();      c.prototype.constructor = c;  }    function f(){};  function e(){};  function d(){};  function c(){};  function b(){};  function a(){};    inherit(e, f);  inherit(d, e);  inherit(c, d);  inherit(b, c);  inherit(a, b);    var = new a();  var c = new c()    document.write( a.constructor + "<br>" );  document.write( c.constructor );  
Comments
Post a Comment