Study on prototype attributes in JScript page 1/2

Source: Internet
Author: User

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:CopyCodeThe 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:Copy codeThe 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:Copy codeThe 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.

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.