Javascript Prototype - Multiple member functions -
i have javascript object pathdiagramelement has few prototype members. there passiveelement object prototype pathdiagramelement.
also passiveelement, has lot of prototype members of own.
var pathdiagramelement = function(){}; pathdiagramelement.prototype = { myself : "parent", gettest : function() { homecoming this.myself + " function"; } }; var passiveelement = function() {}; passiveelement.prototype = new pathdiagramelement(); passiveelement.prototype = { myself : "child", gettestchild : function() { homecoming this.myself+ " function"; } }; var p = new passiveelement(); alert(p.gettestchild()); alert(p.gettest());
p.gettestchild() works fine. p.gettest() throws undefined not function error.
but if change
passiveelement.prototype = { myself : "child", gettestchild : function() { homecoming this.myself+ " function"; } };
to
passiveelement.prototype.myself = "child"; passiveelement.prototype.gettestchild = function() { homecoming this.myself+ " function"; }
everything works fine.
how define object has multiple prototype members of own has utilize prototype of object?
thanks in advance.
you need combination of two. in first case, you're overwriting prototype
of passiveelement
exclusively new object. instead, need create new pathdiagramelement
, assign values new properties on same object, in sec example. this:
passiveelement.prototype = new pathdiagramelement(); passiveelement.prototype.myself = "child"; passiveelement.prototype.gettestchild = function() { homecoming this.myself + " function"; }
there's no way around this. there convenience methods extending existing objects, but, @ end of day, need assign values new properties of existing object.
note next equivalent:
var pathdiagramel = new pathdiagramelement(); pathdiagramel.myself = "child"; pathdiagramel.gettestchild = function() { homecoming this.myself + " function"; } passiveelement.prototype = pathdiagramel;
javascript prototype
No comments:
Post a Comment