JavaScript prototype chain sample sharing _javascript Tips

Source: Internet
Author: User
Tags function prototype

Copy Code code as follows:

<mce:script type= "Text/javascript" ><!--
/*
Each object instance has an attribute member for a prototype (prototype) that points to its instanceof object (tentatively called the parent object)
We refer to the relationship of this layer to the parent archetype as [prototype chain prototype Chian]
The 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 instances of functions.
The parent prototype of a function points to the prototype of the function, and the Function.prototype's parent is the prototype of object
The parent of Object also points to the prototype of the function, Object.prototype is the top-level of all the 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";
}
A.prototype = {
Say:function () {alert (' a ');}
}
Alert (a instanceof function);//a is an instance of function;
Alert (a.__proto__ = = Function.prototype);//a's parent prototype points to the prototype of the Function;
Alert (Function instanceof object);//function is an instance of object;
Alert (function.__proto__ = = Function.prototype);//function's parent prototype points to the prototype of the Function;
Alert (function.prototype.__proto__ = = Object.prototype); The parent of the//function prototype points to the prototype of Object
Alert (object.__proto__ = = Function.prototype);//object's parent prototype points to the prototype of the Function;
alert (object.prototype.__proto__);//object's prototype is the top of all parent prototypes, and it no longer has a parent prototype;

Alert (A.prototype instanceof object);//a's prototype is also an object
Alert (a.prototype.__proto__ = = Object.prototype); The parent of the//A prototype points to the prototype of Object

var A = function () {};
A.prototype = new A ();
A.prototype.say = function () {
Alert (' A ');
}
Alert (A instanceof function);//a is an instance of a function
Alert (a.__proto__ = = Function.prototype);//a's parent prototype points to the Function's prototype
Alert (A.prototype instanceof a); The prototype of//A is an instance of a
Alert (a.prototype.__proto__ = = A.prototype); The parent archetype of the//a prototype points to the prototype of a

var IA = new A ();//ia is an instance of a, ia.__proto__ = = = A.prototype
var IB = new A ();//ib is an instance of a, ib.__proto__ = = = A.prototype
Ia.hi ();
/*
IA itself does not have the Hi method (not in the construction, nor has it been defined),
So find ia.__proto__ that a.prototype, also did not find,
Then find a.prototype.__proto__ that a.prototype, still not found,
Continue to find a.prototype.__proto__ that Object.prototype, wow, found Hi, so call it, stop looking
Output: Hi Object
*/
Ib.hi ();
/*
The IB itself does not have the Hi method (not in construction, nor in its own definition),
So find ib.__proto__ that a.prototype, still not found,
Continue to find a.prototype.__proto__ that Object.prototype, wow, found Hi, so call it, stop looking
Output: Hi Object
*/
A.hi ();
/*
There is no Hi method in itself (not in construction, not in definition),
So find a.__proto__ both Function.prototype, wow, found Hi, so call it, stop looking
Output: Hi Function
*/
Ia.say ();
/*
The IA itself has no say method (no construction, no definition of itself),
So find ia.__proto__ that a.prototype, wow, found the say, so call it, stop looking
So, here's the call to A.prototype.say.
Output: A
*/
Ib.say ();
/*
The IB itself has no say method (no construction, no definition of itself),
So find ib.__proto__ that a.prototype, wow, found the say, so call it, stop looking
So, here's the call to A.prototype.say.
Output: A
*/
Ia.bad ();
/*
IA does not have a bad method in itself (not in construction, nor in its own definition),
So find ia.__proto__ that a.prototype, also did not find,
Then find a.prototype.__proto__ that a.prototype, still not found,
Continue to find a.prototype.__proto__ that Object.prototype, finally is not found, stop looking
Return error, Ia.bad is not a function
*/
--></mce:script>

</script>

Thank Simon for the amendment! All instances do not look for their own prototype when looking for property methods (the instance's prototype is not within the prototype chain, only as a property)!

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.