Examples of JavaScript prototype chain

Source: Internet
Author: User

This article mainly introduces the JavaScript prototype chain example. If you need it, you can refer to it.

The Code is as follows: <mce: script type = "text/javascript"> <! --/* Each object instance has an attribute member pointing to its prototype of the instanceof object (temporarily called the parent object) we call this layer-by-layer relationship to the parent prototype [prototype chian] 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 prototype parent prototype is the Object prototype. The parent prototype of the Object also points to the Function prototype, Object. prototype 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" ;}a. prototype = {say: function () {alert ('A') ;}} alert (a instanceof Function); // a is a Function instance; alert (. _ pr Oto _ = Function. prototype); // The parent prototype of a points to the Function prototype; alert (Function instanceof Object); // Function is the Object instance; alert (Function. _ proto _ = Function. prototype); // The parent prototype of the Function points to the prototype of the Function; 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 pointing to the Function prototype; alert (Object. prototype. _ proto _); // the prototype of the Object is the top of all parent prototypes, It no longer has a parent prototype; alert (. prototype instanceof Object); // a prototype is also an Object alert (. prototype. _ proto _ = Object. prototype); // The parent prototype of a's prototype points to the prototype var A = function () {};. prototype = new a ();. prototype. say = function () {alert ('A');} alert (A instanceof Function); // A is A Function instance alert (. _ proto _ = Function. prototype); // The parent prototype of A points to the Function prototype alert (. prototype instanceof a); // the prototype of A is the alert (. prototype. _ proto _ =. Prototype); // The parent prototype of A's prototype points to the prototype var iA = new a () of A; // iA is an instance of A, iA. _ proto _ =. prototypevar iB = new a (); // iB is an instance of a, iB. _ proto _ =. prototypeiA. hi ();/* iA itself does not have the hi method (not in the construction, or defined by itself), so I am looking for iA. _ proto _ is. prototype cannot be found, so I will try again. prototype. _ proto _ is. prototype, still not found, continue to search for. prototype. _ proto _ is Object. prototype, wow, I found hi, so I called it and stopped searching for the 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 _ is. prototype, still not sent Now, continue to search for. prototype. _ proto _ is Object. prototype, wow, I found hi, so I called it and stopped searching for the output: hi Object */. hi ();/* a itself does not have the hi method (not in the constructor or defined by itself), so find. _ proto _ Function. prototype, wow, I found hi, so I called it and stopped searching for the output: hi Function */iA. say ();/* iA itself does not have the say method (not in the constructor or defined by itself), so iA is called. _ proto _ is. prototype, wow, I found the say, so I called it and stopped searching. So here I called. prototype. say output: A */iB. say ();/* iB itself does not have the say method (not in the constructor or defined by itself), so find iB. _ proto _ is. prototype, wow, I found the say, so I called it and stopped searching. So here I called. prototy Pe. say output: a */iA. bad ();/* iA itself does not have the bad method (not in the constructor or defined by itself), so iA. _ proto _ is. prototype cannot be found, so I will try again. prototype. _ proto _ is. prototype, still not found, continue to search for. prototype. _ proto _ is Object. prototype. Finally, it cannot be found. An error is returned when you stop searching. iA. bad is not a function * // --> </mce: script> </script> thanks to simon for his suggestion! 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 )!

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.