Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
For a description of the object's prototype property, the JScript manual says: All Jscript internal objects have read-only prototype properties. You can dynamically add functionality (properties and methods) to its prototype, but the object cannot be assigned a different prototype. However, user-defined objects can be assigned to a new prototype.
Let's look at three examples of the use of classic prototype properties.
1. Add methods for building objects in script environment:
Array.prototype.max = function ()
var i, max = this;
for (i = 1; i < this.length; i++)
if (Max < this[i])
max = This[i];
2. Adding methods for user-defined classes: function Testobject (name)
This.m_name = Name;
TestObject.prototype.ShowName = function ()
3, update the custom class Prototype:function testobjecta ()
This. MethodA = function ()
Alert (' Testobjecta.methoda () ');
function TESTOBJECTB ()
This. MethodB = function ()
Alert (' Testobjectb.methodb () ');
Testobjectb.prototype = new Testobjecta ();
The third one looks familiar, doesn't it? Yes, it is the prototype inheritance method we introduced earlier ~ ~ But today we are not studying "inheritance", the reason why we can implement a kind of inheritance, just take advantage of a side effect of the prototype attribute.
Prototype also has a default attribute: constructor, which is used to represent the function that created the object (that is, the constructor in our OOP). The constructor property is a member of all objects that have prototype properties. They include all JScript internal objects except the global and math objects. The constructor property holds a reference to a function that constructs a particular object instance.
After figuring out how to use the prototype attribute in JScript, let's look at it in depth.
I've outlined the various uses of the prototype attribute in JScript in the previous article, but prototype is not created by JScript, and JScript actually uses our design patterns prototype A derivative form of pattern. Let me just briefly say prototype pattern, and then look at what is the prototype in JScript?!
What ' s prototype pattern?
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Use the prototype instance to specify the type of object to create and create new objects by copying them.
The prototype pattern allows an object to create another customizable object without knowing any details of how it was created, working by passing a prototype object to the object to be created, which is created by requesting a prototype object to copy themselves.
Continue to understand what is prototype pattern, you can see the ' design mode of the prototype (prototype) ' This article, even if you do not understand Java does not matter, put its code as C # look at the line.
Figure out what the prototype is, right? Anyway, remember, prototype pattern is the implementation relies on the clone of this operation, of course, to shallow copy or deep copy of the clone to see their needs.
Here we go on to say prototype in JScript, why do we say it is different from the prototype in prototype pattern?! This is not what I said, or I blew out, look at this example, you can be roughly confused:
Don't worry, you haven't started the sample yet, just give us a class to demonstrate. What's the RP? RPWT? Of course not, the RP is Researchprototype. Okay, no nonsense, look at the examples and analysis of the results.
The results of the operation are shining:
This%$@#^$%&^ ..., do not worry, continue to see Oh!
Run the results again:
A Runtime Error has occurred.
Do your wish to Debug?
Error:object doesn ' t support this property or method
It's fun, you see what you got? Here the Rp.propertya and Rp.methoda are only used to do the reference, but how to delete the This.propertya and This.methoda, but also the results, but also the prototype imported properties and methods?
This is the biggest difference between JScript's prototype and prototype pattern prototype, and this so-called prototype attribute in JScript is actually a feature supported by the language itself, where no copy is happening, Whether it's shallow or deep. For JScript's interpretation engine, it is processing the "." Or the properties and methods of an object referenced by [KeyName], it is found in the instance of the object itself (this), and returned or executed if it finds it. If no lookup is found, the object and method are defined in the prototype (This.constructor.prototype) of the object, returned or executed if it is located, and returned undefined (for attributes) or runtime error (for method).
Because the properties or methods of prototype import class instances are dynamically searched, we can add prototype properties and methods to the internal objects of the system, such as adding the Trim method to a String object:
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to firstname.lastname@example.org. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
and provide relevant evidence. A staff member will contact you within 5 working days.