Topic: About the Javascript prototype construction mechanism

Source: Internet
Author: User

ECMA is too arrogant. This article attempts to use a simple test example to illustrate the problem.

The prototype of any function is automatically created immediately after it is created:

JS Code
  1. Function CoO (){};
  2. Alert (COO. Prototype. constructor); // => function CoO (){}

Prototype of the instantiated coo is placed at the top of the scope: JS Code

  1. Function CoO (){
  2. Alert (COO. Prototype. constructor );
  3. }
  4. VaR C1 = new COO (); // => function CoO (){..}

Modify the prototype of Coo before instantiating it. The scope is automatically changed to the prototype of the modified prototype at the top of the scope, and the JS Code of method can be found along the prototype chain of instance C1.

  1. Function CoO (){
  2. Alert (COO. Prototype. method); // => function () {} scope automatically changed to the modified prototyp
  3. }
  4. COO. Prototype = {method: function (){}}
  5. VaR C1 = new COO (); // => function (){}
  6. Alert (c1.method); // => function (){}

Re-set the COO'sAttributePrototype: JS Code

  1. Function CoO (){
  2. Alert (COO. Prototype. method); // => the prototype automatically created by default is still at the top of the undefined scope.
  3. COO. Prototype = {method: function (){}}
  4. // Note: The [B] attribute [/B] prototype of coo is reset in the preceding line,
  5. // It is no longer the prototype automatically created by CoO or automatically adjusted. This is the core of the Javascript prototype mechanism.
  6. }
  7. VaR C1 = new COO (); // => function (){}
  8. // Therefore, although it looks like:
  9. Alert (c1.prototype. Method) // => function (){}
  10. // However, this [B] attribute [/B] -- "prototype" () is not another [B] prototype [/B] -- "prototype:
  11. Alert (c1.method); // => undefined

Therefore, during the instantiation processPrototypePrototype to extend or modify the JS Code

  1. Function CoO (){
  2. Alert (COO. Prototype. method); // => undefined
  3. COO. Prototype. method = function () {}; // expands on the prototype of the authentic prototype!
  4. }
  5. VaR C1 = new COO (); // => function (){}
  6. Alert (c1.prototype. method); // => function (){}
  7. Alert (c1.method); // => function (){};

At the bottom, since the COO'sAttributePrototype,
So COO'sPrototypeWhere will prototype go again? JS Code

  1. Function CoO (){
  2. COO. Prototype = {method: function () {alert ("instantiation ");}};
  3. };
  4. COO. Prototype = {constructor: Coo, method: function (){}}
  5. VaR C1 = new COO ();
  6. Alert (c1.prototype. method); // => function () {alert ("instantiation");} bad!
  7. Alert (c1.method); // => function () {} Thank God!
  8. Alert (c1.constructor === COO); // => true; prototype chains are still present, but they seem to be saved internally

There are a lot of worships, in short:
The prototype of a function is automatically created (can be modified or referenced reset) before it is instantiated (or executed by the function itself.
While instantiating (or executing the function itself,ResetPrototype of has become an ordinary object attribute,
However, it does not interfere with the prototype chain lookup mechanism of normal JavaScript instance methods.

Http://www.javaeye.com/topic/453432

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.