Prototype attributes in JScript _ javascript skills: JScriptPrototypeResearch

Source: Internet
Author: User
We know that the prototype attribute of the object in JScript is used to return the reference of the Object Type prototype. 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 "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:

The Code is as follows:

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:

The Code is as follows:

function TestObject(name) { this.m_Name = name; } TestObject.prototype.ShowName = function() { alert(this.m_Name); };


3. Update the prototype of the custom class:

The Code is as follows:

function TestObjectA() { this.MethodA = function() { alert('TestObjectA.MethodA()'); } } 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 attribute: constructor, which is used to represent the function for creating objects (that is, the constructor 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.

In the previous article, I listed the usage of prototype attributes in JScript, but prototype is not created by JScript, JScript actually uses a derivative form of prototype pattern in our design pattern. Next, let's briefly talk about prototype pattern, and then let's take a look at 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 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. The working principle is to pass a prototype object to the object to be created, the objects to be created are created by requesting the prototype objects to copy themselves.
Continue to understand What prototype pattern 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 prototype pattern depends on the clone operation. Of course, it depends on shallow copy or deep copy clone.
Next we will continue to talk about prototype in JScript. Why do we say it is different from prototype in prototype pattern ?! This is not what I just said, or I did not blow it out. Look at this example and you will be confused:

The Code is as follows:

Contact Us

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.