Study of prototype (prototype) attributes in JavaScript

Source: Internet
Author: User
Tags add object constructor continue copy inheritance string trim
JavaScript we know the prototype properties of objects in JScript, which are used to return references to object type prototypes. We use the prototype property to provide a set of basic functions for the object's class. And a new instance of the object "inherits" the action given to the object's prototype. But how exactly is this prototype implemented and managed?

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[0];
for (i = 1; i < this.length; i++)
{
if (Max < this[i])
max = This[i];
}
return Max;
};
2. Adding methods for user-defined classes: function Testobject (name)
{
This.m_name = Name;
}

TestObject.prototype.ShowName = function ()
{
alert (this.m_name);
};
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:
1
Rp. MethodA
100
This. MethodA
This%$@#^$%&^ ..., do not worry, continue to see Oh!


Run the results again:
Undefined
A Runtime Error has occurred.
Do your wish to Debug?
Line:32
Error:object doesn ' t support this property or method
10
RP.prototype.MethodA

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:







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.