Javascript prototype chain

Source: Internet
Author: User

View plaincopy to clipboardprint?
<MCE: Script Type = "text/JavaScript"> <! --
/*
Each object instance has a prototype property member used to point to its instanceof object (temporarily called parent object)
We call this layer-by-layer relationship pointing to the parent prototype [prototype Chian]
A prototype also has a parent prototype, because it is often an object instance, unless we artificially change it
In JavaScript, "Everything is an object, and the function is the first type. "
Both function and object are function instances.
The parent prototype of the function points to the prototype of the function. The parent prototype of function. prototype is the prototype of the object.
The object parent prototype also points to the function prototype, which is the top layer of all parent prototypes.
In the spidermonkey engine, the parent prototype can be accessed through _ PROTO _.
*/
Function. Prototype. Hi = function () {alert ('Hi function ');}
Object. Prototype. Hi = function () {alert ('Hi object ');}
VaR A = function (){
This.txt = "";
}
A. Prototype = {
Say: function () {alert ('A ');}
}
Alert (A instanceof function); // A is a function instance;
Alert (A. _ PROTO _ = function. Prototype); // The parent prototype of a points to the function prototype;
Alert (function instanceof object); // function is an object instance;
Alert (function. _ PROTO _ = function. Prototype); // The parent prototype of the function points to the function prototype;
Alert (function. Prototype. _ PROTO _ = object. Prototype); // The parent prototype of the function prototype directs to the object prototype.
Alert (object. _ PROTO _ = function. Prototype); // The parent prototype of the object directs to the function prototype;
Alert (object. Prototype. _ PROTO _); // the prototype of the object is the top of all parent prototypes, and it no longer has the parent prototype;

Alert (A. Prototype instanceof object); // the prototype of a is also an object
Alert (A. Prototype. _ PROTO _ = object. Prototype); // The parent prototype of a's prototype directs to the object's prototype

VaR A = function (){};
A. Prototype = new ();
A. Prototype. Say = function (){
Alert ('A ');
}
Alert (A instanceof function); // A is a function instance.
Alert (A. _ PROTO _ = function. Prototype); // The parent prototype of a points to the function prototype.
Alert (A. Prototype instanceof A); // the prototype of a is an instance of.
Alert (A. Prototype. _ PROTO _ = A. Prototype); // The parent prototype of a's prototype directs to a's prototype

VaR IA = new A (); // IA is an instance of a, and IA. _ PROTO _ = A. Prototype
VaR IB = new A (); // IB is an instance of a, and IB. _ PROTO _ = A. Prototype
IA. Hi ();
/*
IA itself does not have the HI method (neither in construction nor definition ),
So IA. _ PROTO _ Is A. prototype, and no,
So I found a. Prototype. _ PROTO _, which is a. prototype,
Continue to search for a. Prototype. _ PROTO _, that is, object. Prototype. Wow, I found hi, so I called it and stopped searching.
Output: Hi object
*/
Ib. Hi ();
/*
IB itself does not have the HI method (neither in the construction nor defined by itself ),
So find Ib. _ PROTO _, that is, a. Prototype. No such information is found,
Continue to search for a. Prototype. _ PROTO _, that is, object. Prototype. Wow, I found hi, so I called it and stopped searching.
Output: Hi object
*/
A. Hi ();
/*
A itself does not have the HI method (neither in the construction nor defined by itself ),
Find a. _ PROTO _ function. prototype.
Output: Hi Function
*/
IA. Say ();
/*
IA itself does not have the say method (neither in construction nor definition ),
So I found IA. _ PROTO _, which is a. Prototype. Wow, I found the say, and called it to stop searching.
Therefore, A. Prototype. Say is called here.
Output:
*/
Ib. Say ();
/*
IB itself does not have the say method (neither in the construction nor defined by itself ),
Find Ib. _ PROTO _, that is, a. Prototype. Wow, I found the say, and called it to stop searching.
Therefore, A. Prototype. Say is called here.
Output:
*/
IA. Bad ();
/*
IA itself does not have the bad method (neither in the construction nor defined by itself ),
So IA. _ PROTO _ Is A. prototype, and no,
So I found a. Prototype. _ PROTO _, which is a. prototype,
Continue to search for a. Prototype. _ PROTO _, that is, object. Prototype. Finally, the system cannot find it and stops searching.
Returned error. Ia. Bad is not a function.
*/
// --> </MCE: SCRIPT>
Thanks to Simon for his comments! All instances do not search for their prototype when searching for the attribute method (the prototype of the instance is not in the prototype chain and can only be used as one attribute )!
</SCRIPT>
<MCE: Script Type = "text/JavaScript"> <! --
/*
Each object instance has a prototype property member used to point to its instanceof object (temporarily called parent object)
We call this layer-by-layer relationship pointing to the parent prototype [prototype Chian]
A prototype also has a parent prototype, because it is often an object instance, unless we artificially change it
In JavaScript, "Everything is an object, and the function is the first type. "
Both function and object are function instances.
The parent prototype of the function points to the prototype of the function. The parent prototype of function. prototype is the prototype of the object.
The object parent prototype also points to the function prototype, which is the top layer of all parent prototypes.
In the spidermonkey engine, the parent prototype can be accessed through _ PROTO _.
*/
Function. Prototype. Hi = function () {alert ('Hi function ');}
Object. Prototype. Hi = function () {alert ('Hi object ');}
VaR A = function (){
This.txt = "";
}
A. Prototype = {
Say: function () {alert ('A ');}
}
Alert (A instanceof function); // A is a function instance;
Alert (A. _ PROTO _ = function. Prototype); // The parent prototype of a points to the function prototype;
Alert (function instanceof object); // function is an object instance;
Alert (function. _ PROTO _ = function. Prototype); // The parent prototype of the function points to the function prototype;
Alert (function. Prototype. _ PROTO _ = object. Prototype); // The parent prototype of the function prototype directs to the object prototype.
Alert (object. _ PROTO _ = function. Prototype); // The parent prototype of the object directs to the function prototype;
Alert (object. Prototype. _ PROTO _); // the prototype of the object is the top of all parent prototypes, and it no longer has the parent prototype;

Alert (A. Prototype instanceof object); // the prototype of a is also an object
Alert (A. Prototype. _ PROTO _ = object. Prototype); // The parent prototype of a's prototype directs to the object's prototype

VaR A = function (){};
A. Prototype = new ();
A. Prototype. Say = function (){
Alert ('A ');
}
Alert (A instanceof function); // A is a function instance.
Alert (A. _ PROTO _ = function. Prototype); // The parent prototype of a points to the function prototype.
Alert (A. Prototype instanceof A); // the prototype of a is an instance of.
Alert (A. Prototype. _ PROTO _ = A. Prototype); // The parent prototype of a's prototype directs to a's prototype

VaR IA = new A (); // IA is an instance of a, and IA. _ PROTO _ = A. Prototype
VaR IB = new A (); // IB is an instance of a, and IB. _ PROTO _ = A. Prototype
IA. Hi ();
/*
IA itself does not have the HI method (neither in construction nor definition ),
So IA. _ PROTO _ Is A. prototype, and no,
So I found a. Prototype. _ PROTO _, which is a. prototype,
Continue to search for a. Prototype. _ PROTO _, that is, object. Prototype. Wow, I found hi, so I called it and stopped searching.
Output: Hi object
*/
Ib. Hi ();
/*
IB itself does not have the HI method (neither in the construction nor defined by itself ),
So find Ib. _ PROTO _, that is, a. Prototype. No such information is found,
Continue to search for a. Prototype. _ PROTO _, that is, object. Prototype. Wow, I found hi, so I called it and stopped searching.
Output: Hi object
*/
A. Hi ();
/*
A itself does not have the HI method (neither in the construction nor defined by itself ),
Find a. _ PROTO _ function. prototype.
Output: Hi Function
*/
IA. Say ();
/*
IA itself does not have the say method (neither in construction nor definition ),
So I found IA. _ PROTO _, which is a. Prototype. Wow, I found the say, and called it to stop searching.
Therefore, A. Prototype. Say is called here.
Output:
*/
Ib. Say ();
/*
IB itself does not have the say method (neither in the construction nor defined by itself ),
Find Ib. _ PROTO _, that is, a. Prototype. Wow, I found the say, and called it to stop searching.
Therefore, A. Prototype. Say is called here.
Output:
*/
IA. Bad ();
/*
IA itself does not have the bad method (neither in the construction nor defined by itself ),
So IA. _ PROTO _ Is A. prototype, and no,
So I found a. Prototype. _ PROTO _, which is a. prototype,
Continue to search for a. Prototype. _ PROTO _, that is, object. Prototype. Finally, the system cannot find it and stops searching.
Returned error. Ia. Bad is not a function.
*/
// --> </MCE: SCRIPT>
Thanks to Simon for his comments! All instances do not search for their prototype when searching for the attribute method (the prototype of the instance is not in the prototype chain and can only be used as one attribute )!
</SCRIPT>

 

Source: http://blog.csdn.net/Vanessa219/archive/2009/06/11/4260304.aspx

 

Related Article

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.