The prototype question about JavaScript. _javascript Tips

Source: Internet
Author: User
Prototype

1,
Prototype is associated with clones,
That is, when an instance is created, prototype will clone the member to an instance of that class (function).
Detail: The most common of several built-in objects in the prototype, such as: Array prototype has join, split method,
When you create the array a, VAR a=[1,2], all the methods in the prototype are clone to a.

2. This is an instance pointer to the class (the pointer is a "dynamic binder"). How to understand the dynamic Binder JS this, please refer to my article: http://blog.never-online.net/article.asp?id=117
When the class instance is created, the instance has pre-defined all members that are similar to THIS.P. Also has a member defined in the prototype prototype, and is not overridden if the class internal definition is the same as a definition in prototype:

Look at this example, The This.func defined by Jsclass, and the Func defined in prototype, if there is a member within the Jsclass that is the same as in the prototype, the precedence is this.func when instantiated, but note that the prototype is not rewriting Func, but rather jsclass instance common, although its precedence does not have t His.func high, at the same time, we can also in this way to understand the prototype and class internal definition members:

<script>
function Jsclass () {
THIS.P = "Never-online";
This.func = function () {
Alert (' func ');
}
}
Jsclass.prototype = {
Func:function () {
alert (THIS.P);
}
}
var a = new Jsclass ();
A.func ();
Delete A.func;
A.func ();
</script>

Let's revise the code above. Look like this:
<script>
function Jsclass () {
THIS.P = "Never-online";
This.func = function () {
Alert (' func ');
}
}
Jsclass.prototype = {
Func:function () {
Alert (THIS.P?THIS.P: ' no value ');
}
}
var a = new Jsclass ();
A.func ()//Call internal member
Delete a.func;//here to remove the Func defined within the class
A.func ()//Call prototype member
Delete a.func;//attempt to delete Func again (prototype)
A.func ()//delete invalid (internal Func has been deleted), still printable output
</script>

Note: Members within a class can be deleted with delete, and the definition in the prototype cannot be deleted with the delete instance name. Member name to delete.
When instantiated with prototype: Specify the type of object to create with a prototype instance, and create a new object by copying the prototypes
Which is on the top.
Delete a.func;//here to remove the Func defined within the class
A.func ()//Call prototype member
After that, call A.func () again, when invoked, by calling Prototype.func. Rather than A.func (), this also explains why there is no rewriting of Func defined within Jsclass and Func defined in prototype.

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.