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
- Function CoO (){};
- Alert (COO. Prototype. constructor); // => function CoO (){}
Prototype of the instantiated coo is placed at the top of the scope: JS Code
- Function CoO (){
- Alert (COO. Prototype. constructor );
- }
- 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.
- Function CoO (){
- Alert (COO. Prototype. method); // => function () {} scope automatically changed to the modified prototyp
- }
- COO. Prototype = {method: function (){}}
- VaR C1 = new COO (); // => function (){}
- Alert (c1.method); // => function (){}
Re-set the COO'sAttributePrototype: JS Code
- Function CoO (){
- Alert (COO. Prototype. method); // => the prototype automatically created by default is still at the top of the undefined scope.
- COO. Prototype = {method: function (){}}
- // Note: The [B] attribute [/B] prototype of coo is reset in the preceding line,
- // It is no longer the prototype automatically created by CoO or automatically adjusted. This is the core of the Javascript prototype mechanism.
- }
- VaR C1 = new COO (); // => function (){}
- // Therefore, although it looks like:
- Alert (c1.prototype. Method) // => function (){}
- // However, this [B] attribute [/B] -- "prototype" () is not another [B] prototype [/B] -- "prototype:
- Alert (c1.method); // => undefined
Therefore, during the instantiation processPrototypePrototype to extend or modify the JS Code
- Function CoO (){
- Alert (COO. Prototype. method); // => undefined
- COO. Prototype. method = function () {}; // expands on the prototype of the authentic prototype!
- }
- VaR C1 = new COO (); // => function (){}
- Alert (c1.prototype. method); // => function (){}
- Alert (c1.method); // => function (){};
At the bottom, since the COO'sAttributePrototype,
So COO'sPrototypeWhere will prototype go again? JS Code
- Function CoO (){
- COO. Prototype = {method: function () {alert ("instantiation ");}};
- };
- COO. Prototype = {constructor: Coo, method: function (){}}
- VaR C1 = new COO ();
- Alert (c1.prototype. method); // => function () {alert ("instantiation");} bad!
- Alert (c1.method); // => function () {} Thank God!
- 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