The practical function Analysis _javascript skill of JS Constructor

Source: Internet
Author: User
Copy Code code as follows:

<script> Function.prototype.createInstance = Function () {
var T = function () {};
T.prototype = This.prototype;
T.constructor = this;
var o = new T ();
This.apply (o, arguments);
return o;
}</script>

Say the above code inside T.constructor = This sentence, I feel this sentence has no practical effect,
itself t.constructor should be for funtion, why set it as an example of funtion,
Copy Code code as follows:

<script>
Function.prototype. $extends = function (p) {
this. $super = P;
var fn = function () {};
Fn.prototype = P.prototype;
This.prototype = new FN ();
I added it myself to ensure that the constructor that constructs the subclass instance still points to the constructor function of the subclass
This.prototype.constructor=this;
//-----------------------------
return this;
};
function Animal () {
}
function Cat () {
}
Cat. $extends (Animal);
var bb=new Cat ();
alert (bb.constructor);
But (this.prototype.constructor=this) this approach through the BB object can not be back to the animal prototype
The following statement still returns the Cat function, not the animal
Alert (Bb.constructor.prototype.constructor)
</script>

And the above code, I added 1, fixed the subclass constructor still point to the subclass function, but the object's prototype chain of the back of the original model can not reach the parent class, the solution is
Remove the this.prototype.constructor=this, not set the constructor property on the prototype, but set a constructor property for the instance, the following code
Copy Code code as follows:

<script>
Function.prototype. $extends = function (p) {
this. $super = P;
var fn = function () {};
Fn.prototype = P.prototype;
This.prototype = new FN ();
return this;
};
function Animal () {
}
function Cat () {
This.constructor= Arguments.callee;
}
Cat. $extends (Animal);
var bb=new Cat ();
alert (bb.constructor);
This can be done through the BB object back to the animal prototype
Alert (Bb.constructor.prototype.constructor)
</script>

Finally, the actual effect of constructor is analyzed
Copy Code code as follows:

<script>
Defining functions
var f=function () {
}
True is shown here, because the constructor of F is the prototype property within the Funtion,f _proto_ the prototype that is assigned to the constructor is the prototype of the function
Instanceof checks whether the _proto_ in F has a common node with Function.prototype and returns True if there is one
Alert (f instanceof Function)
Obj is an instance of F
var obj=new f;
The prototype attribute inside obj _proto_ is assigned to F.prototype at New F, apparently F.prototype does not have a common node with Function.prototype, so it shows false
Alert (obj instanceof Function)
In order for obj to become a function instance (obj instanceof Function) to display True
Just need f.prototype=function.prototype.
F.prototype=function.prototype;
But I do not recommend this because the modification of the F.prototype will destroy the Function.prototype, for example, F.prototype.name= "51js" will also add 1 name attributes to the prototype of the function
The correct approach should be the following, so that modifications such as F.prototype.name will not destroy the prototype of the function.
F.prototype=new Function ();
F.prototype.name= "Zhouyang";
/** key is here, again adjust the constructor property to F, maintenance constructor This practice is to ensure that obj correct back to the prototype chain,
* If we want to get the prototype chain inside obj, but we know only obj, we don't know how obj is instantiated, because the _proto_ attribute inside obj is not visible, So we're going to get the inside of obj. The constructor can only be obtained by Obj.constructor, and then the prototype of the constructor is obtained.
*1. If we add the following sentence (F.prototype.constructor=f), back to the original obj prototype chain
* Only back to the 1-layer prototype chain is Obj.constructor.prototype (subclass prototype)-->obj.constructor.prototype.constructor.prototype (still a subclass prototype), This will only be back to the 1-storey prototype chain
**/
F.prototype.constructor=f;
Obj=new F;
Alert ("Found a subclass---" \ n "+obj.constructor+"
+ "found or subclass, unable to find parent class---" +obj.constructor.prototype.constructor)
Alert (obj instanceof Function)
/**2. If we use the following method to set the constructor of an instance of F in the F definition, instead of the F prototype constructor
* Can be back to the 2-tier prototype chain is Obj.constructor.prototype (subclass prototype)-->obj.constructor.prototype.constructor.prototype (parent class prototype)
* Obviously, this situation is consistent with the object prototype inheritance chain.
*/
F=function () {
This.constructor=arguments.callee;
}
F.prototype=new Function ();
F.prototype.name= "Zhouyang";
Obj=new F;
Alert ("Found a subclass---" \ n "+obj.constructor+"
+ "Find the parent class---" +obj.constructor.prototype.constructor)
Alert (obj instanceof Function)
</script>

Copy Code code as follows:

<script>
Defining functions
var f=function () {
}
True is shown here, because the constructor of F is the prototype property within the Funtion,f _proto_ the prototype that is assigned to the constructor is the prototype of the function
Instanceof checks whether the _proto_ in F has a common node with Function.prototype and returns True if there is one
Alert (f instanceof Function)
Obj is an instance of F
var obj=new f;
The prototype attribute inside obj _proto_ is assigned to F.prototype at New F, apparently F.prototype does not have a common node with Function.prototype, so it shows false
Alert (obj instanceof Function)
In order for obj to become a function instance (obj instanceof Function) to display True
Just need f.prototype=function.prototype.
F.prototype=function.prototype;
But I do not recommend this because the modification of the F.prototype will destroy the Function.prototype, for example, F.prototype.name= "51js" will also add 1 name attributes to the prototype of the function
The correct approach should be the following, so that modifications such as F.prototype.name will not destroy the prototype of the function.
F.prototype=new Function ();
F.prototype.name= "Zhouyang";
/** key is here, again adjust the constructor property to F, maintenance constructor This practice is to ensure that obj correct back to the prototype chain,
* If we want to get the prototype chain inside obj, but we know only obj, we don't know how obj is instantiated, because the _proto_ attribute inside obj is not visible, So we're going to get the inside of obj. The constructor can only be obtained by Obj.constructor, and then the prototype of the constructor is obtained.
*1. If we add the following sentence (F.prototype.constructor=f), back to the original obj prototype chain
* Only back to the 1-layer prototype chain is Obj.constructor.prototype (subclass prototype)-->obj.constructor.prototype.constructor.prototype (still a subclass prototype), This will only be back to the 1-storey prototype chain
**/
F.prototype.constructor=f;
Obj=new F;
Alert ("Found a subclass---" \ n "+obj.constructor+"
+ "found or subclass, unable to find parent class---" +obj.constructor.prototype.constructor)
Alert (obj instanceof Function)
/**2. If we use the following method to set the constructor of an instance of F in the F definition, instead of the F prototype constructor
* Can be back to the 2-tier prototype chain is Obj.constructor.prototype (subclass prototype)-->obj.constructor.prototype.constructor.prototype (parent class prototype)
* Obviously, this situation is consistent with the object prototype inheritance chain.
*/
F=function () {
This.constructor=arguments.callee;
}
F.prototype=new Function ();
F.prototype.name= "Zhouyang";
Obj=new F;
Alert ("Found a subclass---" \ n "+obj.constructor+"
+ "Find the parent class---" +obj.constructor.prototype.constructor)
Alert (obj instanceof Function)
</script> conclusion The function of constructor is to maintain the prototype chain of object

To the fruit and winter enlighten, I do not know whether the correct ah, and I think we often say that the prototype of pollution in the end is what??
function, the following may explain
Copy Code code as follows:

<script>
var f = function (x) {}
f.prototype={};
Alert ((new F). constructor);
F.prototype.constructor=f;
Alert ((new F). constructor);
</script>

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.