storkCore

How Cloning Works
Login

How Cloning Works

Javascript's object model is loosely based on the older programming language, Self. However, many frameworks build class systems most likely to fit with the mental model programmers have from other mainstream languages. However, a prototypical object system is actually more powerful (demonstrated by the fact you can create class-based systems on top of it), while simultaneously being conceptually simpler. Javascript itself lacks the ability to easily create clones from objects, that inherit from the prototype, but with a bit of effort that functionality can be built, and makes an appearance in storkCore.

This means many simple cases can be built without worrying about inheritance at all, simply by creating objects. More complex cases can then have objects inherit from one another. Indeed there is flexibility as one could create an object Pixel with properties x, y, and colour, then either use that directly as a singular object (many simple storkCore examples do that with core objects, with no need for further inheritance or cloning), or one could create a clone of that called, say, PinkPixel with given co-ordinates and colour. Clones from PinkPixel could have their own X and Y co-ordinates, but share the colour. If PinkPixel's colour is changed, all the pink pixels have their colour property changed in sync.

To read more about Self, which inspired this model for storkCore, go to the Self homepage.

Below is a simple example demonstrating how this happens, inside storkCore.

     var ob1 = clone(Stork);
     ob1.text = "Foo";
     ob2 = clone(ob1);
     console.debug(ob2.text);

Displays Foo. Note the use of the clone function to create a new clone that inherits from the original prototype. The root object for storkCore is Stork. ob2 inherits the property from ob1.

     ob1.text = "Bar";     
     console.debug(ob2.text);

Displays Bar. If the property is changed in the object that was cloned, it affects the clones of that object.

     ob2.text = "Fight";
     console.debug(ob1.text);
     console.debug(ob2.text);

Displays Bar followed by Fight. The property was changed for the clone (ob2), but it did not affect the object it was cloned from (ob1).