I am trying to add a new property to a class in an existing JavaScript framework (Wireit).
The base class is called Container and I want all Containers to have a GUID property. Container is currently sub-classed to ImageContainer, FormContainer etc.
If I extend Container to have a GUID, would the GUID be available in ImageContainer, FormContainer etc.?
How would I do this? Examples?
The way prototypical OO works is that when you :
imageContainer
ImageContainer.prototype
)undefined
.This means if you add a property
GUID
Container.prototype.GUID = 42;
All objects with
Container.prototype
in the prototype chain will share that property.If you do not want that property to be shared then it's very difficult to add a unique GUID to all objects with
Container.prototype
in their prototype chain.You can however use
.isPrototypeOf
to check whether an object has a prototype in their chain.Example:
This however relies on
objs
with all the objects you might want to extend.As already described, if GUID is a function you can simply add it to the container prototype. If not, adding this function
Will allow you to access a GUID property by using
obj.GUID ()
or set it usingobj.GUID (val)
If i understood you well, here is an example:
http://jsfiddle.net/mMfdj/1/
And here with automatic unique GUID's:
http://jsfiddle.net/mMfdj/5/