SoFunction
Updated on 2025-04-07

Analysis of the actual role of js constructor

Copy the codeThe code is as follows:

<script> = function(){
var T = function(){};
= ;
= this;
var o = new T();
(o, arguments);
return o;
}</script>

Let’s talk about the sentence = this in the above code. I feel that this sentence has no practical effect.
It should be Funtion itself, why should it be set as an instance of Funtion?
Copy the codeThe code is as follows:

<script>
.$extends = function(p){
this.$super = p;
var fn = function(){};
= ;
= new fn();
//I added this sentence myself to ensure that the constructor that constructs the subclass instance still points to the subclass constructor function
=this;
//-----------------------------
return this;
};
function Animal(){
}
function Cat(){
}
Cat.$extends(Animal);
var bb=new Cat();
alert();
//However (=this) This method cannot return to Animal's prototype through the bb object
//The following statement still returns the function Cat, not Animal
alert()
</script>

There is also the above code. I added 1 sentence myself, correcting that the subclass constructor still points to the subclass function, but the regression of the object's prototype chain cannot reach the parent class prototype. The solution is
Remove = this; neither set the constructor attribute for the prototype, but set a constructor attribute for the instance, as follows
Copy the codeThe code is as follows:

<script>
.$extends = function(p){
this.$super = p;
var fn = function(){};
= ;
= new fn();
return this;
};
function Animal(){
}
function Cat(){
= ;
}
Cat.$extends(Animal);
var bb=new Cat();
alert();
//This method can be used to revise the Animal prototype through the object bb
alert()
</script>

Finally, analyze the actual role of constructor
Copy the codeThe code is as follows:

<script>
//Define the function
var f=function(){
}
//The true is displayed here, because the constructor of f is Function, the prototype property_proto_ inside f is assigned as the prototype of the constructor, which is the prototype of the Function, which is the prototype of the function
//Instanceof checks whether _proto_ inside f has a common node, and if so, return true
alert(f instanceof Function)
//obj is an instance of f
var obj=new f;
//The prototype attribute _proto_ inside obj is assigned as in new f, obviously has no common node, so it shows false
alert(obj instanceof Function)
//In order to make obj an instance of Function, the obj instance of function displays true
//Only need =
=;
//But I do not recommend the above approach, because the correct modification will be broken. For example, ="51js" will also add 1 name attribute to the prototype of the Function.
//The correct way should be as follows, so that modifications such as this will not destroy the prototype of the Function
=new Function();
="zhouyang";
/**The key is here, adjust the constructor attribute to f again and maintain the constructor to ensure that obj can correctly recall the prototype chain.
*If we want to obtain the prototype chain inside obj, but we only know obj, but we don’t know how obj is instantiated. Since the _proto_ attribute inside obj is not visible, then we can only obtain the constructor through the prototype of obj, and then obtain the constructor's prototype.
*1. If we add the following sentence (=f), we will revise the obj prototype chain
*Only retroactive to the 1-layer prototype chain, that is, (subclass prototype) --> (still subclass prototype), so that you can only retroactive to the 1-layer prototype chain
**/
=f;
obj=new f;
alert("Subclass found ---"++"\n"
+"The subclass is found, the parent class cannot be found ---"+)
alert(obj instanceof Function)
/**2. If we use the following method to set the constructor of the instance of f in the f definition, instead of the constructor of the f prototype in the f definition
* You can go back to the second layer prototype chain, that is (subclass prototype) --> (parent class prototype)
*Obviously this situation is in line with the case of the object prototype inheritance chain
*/
f=function(){
=;
}
=new Function();
="zhouyang";
obj=new f;
alert("Subclass found ---"++"\n"
+"The parent class was found ---"+)
alert(obj instanceof Function)
</script>

Copy the codeThe code is as follows:

<script>
//Define the function
var f=function(){
}
//The true is displayed here, because the constructor of f is Function, the prototype property_proto_ inside f is assigned as the prototype of the constructor, which is the prototype of the Function, which is the prototype of the function
//Instanceof checks whether _proto_ inside f has a common node, and if so, return true
alert(f instanceof Function)
//obj is an instance of f
var obj=new f;
//The prototype attribute _proto_ inside obj is assigned as in new f, obviously has no common node, so it shows false
alert(obj instanceof Function)
//In order to make obj an instance of Function, the obj instance of function displays true
//Only need =
=;
//But I do not recommend the above approach, because the correct modification will be broken. For example, ="51js" will also add 1 name attribute to the prototype of the Function.
//The correct way should be as follows, so that modifications such as this will not destroy the prototype of the Function
=new Function();
="zhouyang";
/**The key is here, adjust the constructor attribute to f again and maintain the constructor to ensure that obj can correctly recall the prototype chain.
*If we want to obtain the prototype chain inside obj, but we only know obj, but we don’t know how obj is instantiated. Since the _proto_ attribute inside obj is not visible, then we can only obtain the constructor through the prototype of obj, and then obtain the constructor's prototype.
*1. If we add the following sentence (=f), we will revise the obj prototype chain
*Only retroactive to the 1-layer prototype chain, that is, (subclass prototype) --> (still subclass prototype), so that you can only retroactive to the 1-layer prototype chain
**/
=f;
obj=new f;
alert("Subclass found ---"++"\n"
+"The subclass is found, the parent class cannot be found ---"+)
alert(obj instanceof Function)
/**2. If we use the following method to set the constructor of the instance of f in the f definition, instead of the constructor of the f prototype in the f definition
* You can go back to the second layer prototype chain, that is (subclass prototype) --> (parent class prototype)
*Obviously this situation is in line with the case of the object prototype inheritance chain
*/
f=function(){
=;
}
=new Function();
="zhouyang";
obj=new f;
alert("Subclass found ---"++"\n"
+"The parent class was found ---"+)
alert(obj instanceof Function)
</script>Conclusion The function of constructor is to maintain the prototype chain of the object

I would like to give some advice to Guoguo and Winter, I don’t know if the understanding is correct. In addition, I see what the pollution of the prototype refers to as the commonly mentioned prototype? ?
If it works, the following may explain
Copy the codeThe code is as follows:

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