What is prototype


A single object serves for all instances of that Class

Every constructor has a prototype object. When a new instance is created through this constructor, the prototype object is assigned as the __proto__ of the new instance. 

All the instances created through this constructor point to this single one prototype object as its __proto__.  This single one prototype object serves as a back up of all (maybe hundreds) instances of this Class.

P={x:3,y:5}
C=function(){}
C.prototype=P;
c1=new C();
trace(c1.x);// output 3;

c1 object is created through C constructor. So, c1.__proto__=P;  c1 is born with the property x to be 3 through __proto__.

Usually the syntax will be straight forward as:

C=function(){}
C.prototype.x=3;
C.prototype.y=5;

Never the less, it is helpful to think that prototype is an independent object. It does not live or die along wtih constructor.

Note that, every constructor has already an object as prototype object. We need not say C.prototype=new Object();

Since we have discussed the __proto__ thing, it shoud be clear enough what prototype is.


Extend the properties and methods of the prototype object

We can give MovieClip more methods than Flash support. Since all movieClips point their __proto__ to a single backup object (prototype object), we just enforce that prototype object. We add a power to prototype object then all MovieClips will be equipped with this power.

MovieClip.prototype.move=function(){this._x+=5;}

Now, for any movieClip, it has the method "move()" to move rightward. Just say "mc1.move();"

In the era of Flash 5, many were unsatisfied of the String.split() function. So, they write custom codes to over-ride it.

String.prototype.split=function(){// script here};

Then we can say: arr=("ABCD").split();

Here are some discussions:

1. We are modify/enhance the existing Class itself. We are not "sublcass" it. There is no "new Class" nor "inheritance".

2. All instances of that Class will be affected. Those instances creatd before we modify the prototype will also affected. Because the __proto__ of them also points to this prototyp object.

3. Instances can only execute this method after we enforce the prototype. So, it is common to modify prototype as early as possible (usually in frame 1 of main timeline).

Here is a non-sense experiment: I dont think it would be practical.

MovieClip.prototype.onEnterFrame=function(){this._x+=5;}

After this script, all new or old movieClips will starts to move right ward.

Are there any exceptions ?

Yes.  __proto__ only effects when there is no such property in local. Those movieClip that has already got onEnterFrame defined will not be affected.


What if we delete constructor and its prototype object ?
Will this affects functions of those instances ?

If we change or delete the prototype, will it affects instances already created ?

NO. It will only affect instances created since now.

The prototype object is an independent object. We give it a name as prot1. Constructor.prototype property points to this prot1 object. All instances points to this prot1 object.

Then we delete or change the prototype reference. The prot1 object is still there. All instances still have __proto__ pointing to this prot1 object. All the functions still exists and available by the instance.

When we read a property of an instance, it search through this __proto__ reference directly. It does not target the constructor to get what is __proto__.

P={x:5}
C=function(){}
C.prototype=P;

c1=new C();
P={x:99}
C.prototype=P;
trace(c1.x);// output 5

c2=new C();
trace(c2.x);// output 99