JavaScript prototype chain sample sharing

Source: Internet
Author: User
Tags continue

  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)!  
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.