We know that the prototype attribute of the object in JScript is used to return the reference of the Object Type prototype. We use the prototype attribute to provide a set of basic functions of the object class. In addition, the new instance & quot; inherits & quot; and grants the operation of the object prototype. But how exactly is this prototype?
We knowPrototype attributesIs usedReturns the reference of an object type prototype.. We use the prototype attribute to provide a set of basic functions of the object class. In addition, the new instance "inherit" of the object grants the operation of the object prototype. But how is this prototype implemented and managed?
For the description of the prototype attribute of an object, the JScript manual says that all internal JScript objects have the read-only prototype attribute.You can dynamically add features (attributes and methods) to the prototype, but the object cannot be assigned different prototypes.However, user-defined objects can be assigned to new prototypes.
The following shows three examples of using the classic prototype attribute.
1. Add methods for built-in objects in the script environment:
Program code
Array. prototype. max = function ()
{
Var I, max = this [0];
For (I = 1; I <this. length; I ++)
{
If (max <this [I])
Max = this [I];
}
Return max;
};
2. Add methods for user-defined classes:
Program code
Function TestObject (name)
{
This. m_Name = name;
}
TestObject. prototype. ShowName = function ()
{
Alert (this. m_Name );
};
3. Update the prototype of the custom class:
Program code
Function TestObjectA ()
{
This. MethodA = function ()
{
Alert ('testobjecta. MethodA () '); // display the text.
}
}
Function TestObjectB ()
{
This. MethodB = function ()
{
Alert ('testobjectb. MethodB ()');
}
}
TestObjectB. prototype = new TestObjectA ();
Third, are you familiar with it? Yes, it is the prototype inheritance method we introduced earlier ~~ However, today we are not studying "inheritance". The reason why we can implement a kind of inheritance is to take advantage of a side effect of the prototype attribute.
Prototype also has a default property: constructorIs used to represent the functions used to create objects (the constructor we mentioned in OOP ). The constructor attribute is a member of all objects with the prototype attribute. They include all internal JScript objects except Global and Math objects. The constructor attribute stores references to the functions used to construct a specific object instance.
After figuring out how to use the prototype attribute in JScript, we will study it in depth.
The previous article lists the usage of prototype attributes in JScript, but prototype is not created by JScript, JScript is actually a derivative form of prototypepattern in our design pattern. Next, let's briefly talk about prototypepattern, and then let's take a look at the prototype in JScript ?!
What's 'sprototype pattern?
Specifythe kinds of objects to create using a prototypical instance, andcreate new objects by copying this prototype.
Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.
The prototype mode allows an object to create another custom object without any details on how to create it,Working principle: by passing a prototype object to the object to be created, the object to be created is created by requesting the prototype object to copy themselves.
Continue to understand what prototypepattern is. Refer to the article 'prototype of design pattern (Prototype) '. It doesn't matter even if you don't understand Java, let's just look at all its code as C.
Have you figured out what the prototype is? Remember that prototypepattern depends on the clone operation. Of course, it depends on shallowcopy or deepcopy clone.
Next we will continue to talk about prototype in JScript. Why do we say it is different from prototype in prototypepattern ?! This is not what I just said, or I did not blow it out. Look at this example and you will be confused:
Program code