This article mainly introduces the JavaScript prototype chain example, the need for friends can refer to the
Code as follows: <mce:script type= "Text/javascript" ><!--/ Each object instance has an attribute member that is used to point to its instanceof object Prototype (prototype) We refer this layer to the parent prototype as the [prototype chain prototype Chian] Prototype also has a parent prototype because it is often also an object instance. Unless we change it artificially in JavaScript, "Everything is an object, the function is the first type." " function and object are examples of functions. function's parent prototype points to the prototype of the function, and the Function.prototype's parent prototype is the object's prototype object's parent prototype also points to the function's prototype. Object.prototype is the top level of all parent prototypes in the SpiderMonkey engine, the parent prototype can be accessed via __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//function's prototype points to the prototype alert of Object (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 prototype of a has a parent prototype that points to object's prototype 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.__proto__ = = = Function.prototype);//a's parent prototype is directed to the Function's prototype alert (A.prototype instanceof a);//a's prototype is instance alert for a (A.prototype.__proto = = = = = = = = = = = = A.prototype);//a's prototype's parent prototype points to A's prototype var IA = new A ();//ia is an instance of a, ia.__proto__ = = a.prototype var IB = new A ();//ib is an example of a, ib.__proto__ = = A.prototype Ia.hi (); /* IA itself does not have Hi method (constructs not, oneself also did not define), so find ia.__proto__ namely A.prototype, also did not find, then find a.prototype.__proto__ namely A.prototype, still did not find, Continue to find a.prototype.__proto__ that Object.prototype, wow, found Hi, so call it, stop looking for output: Hi Object * * Ib.hi (); /* IB BookThe body does not have the Hi method (constructs not, oneself also did not define), therefore looks for ib.__proto__ namely A.prototype, still did not discover, continues to search a.prototype.__proto__ That is, Object.prototype, wow, found Hi, so call it, stop looking for output: Hi Object/A.hi (); /* A itself does not have the Hi method (construction does not have defined itself), so find a.__proto__ both Function.prototype, wow, found Hi, so call it, stop looking for output: Hi Function */Ia.say (); /* IA itself does not have say method (constructs not, oneself also did not define), so find ia.__proto__ namely A.prototype, wow, found say, so call it, stop looking so, here is the A.prototype.say output: A * * Ib.say (); /* IB itself does not have say method (constructs not, oneself also did not define), so find ib.__proto__ namely A.prototype, wow, found say, so call it, stop looking so, here is the A.prototype.say output: A * * Ia.bad (); /* IA itself does not have bad method (structure does not, oneself also did not define), so find ia.__proto__ namely A.prototype, also did not find, then find a.prototype.__proto__ namely A.prototype, still did not find, Continue to find a.prototype.__proto__ that object.prototype, finally can not find, stop lookup 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)!