JavaScript has a powerful prototype mechanism that is based on prototype and object scripting language.
In JavaScript, there is no "class" concept, so it is not an object-oriented language, but we can use the prototype mechanism we can generate objects to an object-oriented mechanism by constructing a function.
Function base ()
{
Base.prototype.c = function ()
{
Alert ('c');
}
Base.prototype.a = function ()
{
Alert ('a in base');
}
Base.prototype.tostring = function ()
{
Return ("Base");
}
Base.Prototype.base = new object ();
this.d = function ()
{
Alert ('d');
}
}
VAR a = new base ();
VAR b = new base ();
The above code defines a base "class" and constructs two instances of A, B. However, it is noted that although the surface is through the class constructor, JavaScript is actually constructed, the difference is that when the new base () operation is executed twice, the program is actually the base.prototype. * Two assignments. These assignments will bring some additional overhead, so that JavaScript is less efficient than the language of the true face-to-object language. However, this will also get some flexibility.
Function myclass ()
{
IF (MyClass.instanceCount == NULL)
{
Myclass.instancecount = 1;
}
Else myclass.instancecount ;
Myclass.prototype.instancecount = myclass.instancecount;
}
VAR a = new myclass ();
Alert (a.instancecount);
VAR B = new myclass ();
Alert (B.instanceCount);
Alert (a.instancecount);
The above code is dynamically changing the class and object (not just class!) Attribute when constructing the object, so when two objects are constructed, the value of the a.instancecount and the B.instanceCount property is 2.
Objlist = new object ();
Objlist.member = listmember;
Objlist.membercount = 0;
Function ListMember (OBJ)
{
IF (listmember.prototype.head == null)
{
Listmember.prototype.Head = this;
}
IF (ListMember.Prototype.end! = null)
{
This.Prev = this.nd;
THIS.PREV.NEXT = THIS;
}
Listmember.prototype.end = this;
THIS.Object = OBJ;
Objlist.membercount ;
}
VAR newmember = new objlist.member (new object (1));
Newmember = new objlist.member (new object (2)); new bemember = new objlist.member (New Object (3));
Alert (Newmember.Object);
Alert (Newmember.Prev.Object);
Alert (newmember.head.object);
Alert (Objlist.membercount);
This is a more interesting code that constructs new members through a globally unique object Objlist, and adds this new member directly to the tail of the Objlist in the form of a linked list. Note that once a new member is constructed, all object's END attributes (instead of class) will change. From this code, you can also see the differences of Prototype and THIS. When constructing the object, the assignment of the prototype property does not assign a new memory unit unless the value of the attribute is empty. The assignment of this will allocate the new memory unit to the current object. Therefore, changing the properties of the Prototype reference will cause the properties of all such objects in the survival period to change, and the attributes that the THIS reference will only change the current object's properties.
Function MC2 (VAL)
{
THIS.A = VAL;
Mc2.Prototype.b = val;
}
VAR T1 = New MC2 ("X");
Alert (t1.a); // x
Alert (t1.b); // x
VAR T2 = New MC2 ("y");
Alert (t1.a); // x
Alert (t1.b); // y
Alert (t2.a); // y
Alert (t2.b); // y
As seen above, since the mc2.prototype.b properties are referenced since T1.B and T2.B, the value of Mc2.Prototype is actually changed when T2 is constructed, so that the value of t1.b is also A change has changed. Therefore, when the attribute of the class needs to change as the object change, it should be defined, in turn, in turn, when defining a constant or class method, when the running period does not change with the object, it should be used to save space to save space. Still reminding again, although Prototype is usually used to define the properties and methods of "difficult to change", but they can still be changed, and each time constructed a new object. It is important to note that if this.x and prototype.x properties are defined in a class, the former will overwrite the latter.
Using prototype to simulate the inheritance of the object.
Function base ()
{
Base.prototype.c = function ()
{
Alert ('c');
}
Base.prototype.a = function ()
{
Alert ('a in base');
}
Base.prototype.tostring = function ()
{
Return ("Base");
}
Base.Prototype.base = new object ();
}
Function Derived ()
{
Derived.prototype.a = function ()
{
Alert ('a in derived');
}
Derived.prototype.tostring = function ()
{
Return ("Derived");
}
Derived.prototype.test = function ()
{
Alert (this);
}
Derived.prototype.base = new base (); Derived.Prototype = new base ();
In the above code, the DeriveD class inherited the Base class by assigning the prototype.
VAR x = new base ();
Var y = new derived ();
Y.a (); // a in Dervide
Alert (Y.toString ()); // Derived
Y.BASE.A (); // a in base
Alert (Y.BASE.TOSTRING ()); // base
Alert (Y.Base.base.tostring ()); // [Object Object]
Ya (), y.toString () covers the same name method, y.base.a (), yc (), y.base.tostring () calls the same name method, Y.Base.Base .tostring () calls the toString method for the Object class because the Object class is a superclass for all JavaScript classes.
As can be seen from the example, the essence of JavaScript class is simulated by rewriting the prototype property, it is important to note that the implementation of the inheritance will also bring some "side effects", such as changing the value of the constructor field in the object, when Derived When the class inherits from the Base class, the constructor property of the DeriveD object will return the constructor of the BASE object. Of course, this feature can also be interpreted as "the constructor of the Base object" during the constructor of the DeriveD object ", and the fact that this is not a big problem, in the above code, although C. Constance returns Base, but c instanceof The value of Derive is still True. However, in addition to this, since it is changed Prototype, it is necessary to note that the rewriting of Prototype itself must appear at the foremost in the execution order. However, I found that even if I put the prototype's assignment in the first line of the constructor, I can't work. This looks a bit weird. And my understanding is that in the constructor, the prototype attribute is read-only. So my suggestion is to put the assignment of class prototype to the function. (Recommended after ending the brackets), and the value of other members should be written inside the function body.
Finally, remind a thing that is worth noting, that is, JavaScript does not fully simulate the inheritance mechanism, because it is more difficult to implement the inheritance of class properties, at least I have no better scheme. If a person has studied such a problem, please advise, grateful.