Copy code code as follows:
<mce: script type = "text/javascript"> <!-
/*
Each object instance has a prototype (Prototype) that attribute members are used to point to its Instanceof object (temporarily called the parent object)
We point the relationship between the Father's prototype as [prototype chain prototype chian]
The prototype also has a father -in -law, because it is often an object instance, unless we personally change it
In JavaScript, "Everything is an object, the function is the first type."
Function and objects are instances of functions.
Function's father prototype points to the prototype of Function, Function.prototype's father prototype is Object
Object's parent prototype also points to the prototype of the function. Object.prototype is the top layer of all parent prototype
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 the function;
Alert (a .__Loto___ === function.prototype); // A's parent prototype pointed to the prototype of the function;
Alert (function instanceof object); // Function is an instance of Object;
Alert (function .__Loto__ === Fu Conction.prototype); // Function's parent prototype points to the prototype of the function;
Alert (function.prototype .__Loto__ === Object.prototype); // Function's prototype of the prototype pointed to the prototype of Object
Alert (Object .__ Proto__ === Function.prototype); // Object's parent prototype points to the prototype of the function;
Alert (Object.prototype .__Loto __); // Object's prototype is the top of all parent prototypes, it no longer has the original father;
Alert (A.Prototype Instanceof Object); // A's prototype is also an object
Alert (a.prototype .__Loto__ === Object.prototype); // A prototype of the prototype pointed 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 the function
Alert (a .__ propo___ === function.prototype); // A's parent prototype pointed to the prototype of the function
Alert (A.prototype Instanceof A); // A's prototype is an instance of A
Alert (a.prototype .__Loto__ === A.prototype); // A prototype of the prototype pointed 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 has no Hi method (no in the structure, and I have not defined it),
So look for IA .__ Proto__, that is, A.Prototype, and did not find it.
So look for A.Prototype .__Loto__, that is, A.Prototype, and I still didn't find it.
Continue to find A.prototype .___Loto__, that is Object.prototype, wow, I found Hi, so I called it and stop looking for
Output: Hi Object
*/
IB.HI ();
/*
IB itself has no Hi method (no in the structure, and I have not defined it),
So find ib .__loto__, that is, A.Prototype, but still not found it.
Continue to find A.prototype .___Loto__, that is Object.prototype, wow, I found Hi, so I called it and stop looking for
Output: Hi Object
*/
a.Hi ();
/*
A itself does not have a Hi method (there is no in the structure, and I have not defined it),
So find a .__ propo___ both function.prototype, wow, I found Hi, so I called it and stop looking for
Output: Hi Function
*/
ia.say ();
/*
IA itself does not have a saying (no in the structure, and I have not defined it),
So find IA .__ Proto__, that is A.Prototype, wow, I found Say, so I called it and stop looking for
So, here is a.prototype.say
Output: A
*/
IB.Say ();
/*
IB itself does not have a say (no in the structure, and I have not defined it),
So find ib .__Loto__, that is A.Prototype, wow, I found Say, so I called it and stop looking for
So, here is a.prototype.say
Output: A
*/
ia.bad ();
/*
IA itself does not have a BAD method (there is no in the structure, and I have not defined it),
So look for IA .__ Proto__, that is, A.Prototype, and did not find it.
So look for A.Prototype .__Loto__, that is, A.Prototype, and I still didn't find it.
Continue to find A.prototype .__Loto__, that is Object.prototype.
Return error, IA.BAD is not a function
*/
// -> </mce: script>
</script>
Thanks to Simon's correction opinion! All instances do not find their own prototype when searching the attribute method (the prototype of the instance is not in the prototype chain, it can only be used as a attribute)!