prototype programming - Javascript: Is it possible to inject new functions into a base 'class/object' that will be available in all sub-'classes/objects'? -
i trying add new property class in existing javascript framework (wireit).
the base class called container , want containers have guid property. container sub-classed imagecontainer, formcontainer etc.
if extend container have guid, guid available in imagecontainer, formcontainer etc.?
how this? examples?
the way prototypical oo works when :
- look property on object,
imagecontainer
- it properties on object,
- if can't find on prototype (
imagecontainer.prototype
) - if can't find on next prototype ('container.prototype')
- and next prototype ('object.prototype') until prototype chain empty in case returns
undefined
.
this means if add property guid
container.prototype.guid = 42;
all objects container.prototype
in prototype chain share property.
if not want property shared it's difficult add unique guid objects container.prototype
in prototype chain.
you can use .isprototypeof
check whether object has prototype in chain.
example:
var objs = [..]; // objects objs.foreach(function(v) { if (container.prototype.isprototypeof(v)) { v.guid = window.guid++; } });
this relies on
- es5. use es5-shim
- you being able create array
objs
objects might want extend.
Comments
Post a Comment