javascript - Iterate Constructor Chain Up -
assuming have this:
function a() {} function b() {} b.prototype = object.create(a.prototype); function c() {} c.prototype = object.create(b.prototype); var inst = new c();
i can inst instanceof c == true, inst instanceof b == true, instanceof c == true.
but how "iterate" constructor functions starting instance of c() it'd return function c(), function b(), function a() use instantiate instance.
you can iterate prototypes doing
for (var o=inst; o!=null; o=object.getprototypeof(o)) console.log(o); // {} // c.prototype // b.prototype // a.prototype // object.prototype
however, iterate prototype chain. there no such thing "constructor chain". if want access constructors, need set .constructor
property on prototypes appropriately when inheriting:
function a() {} function b() {} b.prototype = object.create(a.prototype); b.prototype.constructor = b; function c() {} c.prototype = object.create(b.prototype); c.prototype.constructor = c; var inst = new c(); (var c=inst.constructor; c!=null; (c=object.getprototypeof(c.prototype)) && (c=c.constructor)) console.log(c); // c // b // // object
which use instantiate instance
you need know of c
that, not of "chain". may access via inst.constructor
if have set c.prototype.constructor
correctly.
however, might bad idea instantiate objects arbitrary constructors; don't know required parameters. don't know actually want do, request might hint @ design flaw.
Comments
Post a Comment