Understanding of javascript Constructor and Prototype Object

  • 2021-07-10 18:48:27
  • OfStack

The following is an example of a constructor

If it is an instance method, the addresses referenced by different instantiations are different and only 1.


// Definition 1 Constructor 
 function People(name,age){
  this.name=name;
  this.age=age;
  this.dothings=function(){
   retrun this.name+this.age;
   }
 }
 var people1 = new People("panda1",25);
 var people2 = new People("panda2",26);
 alert(people1.name==people2.name) //false

Another way to write prototype objects is to create prototype objects literally, where {} is an object, which is Object, new Object relative to {}


 People.prototype={
 constructor:People // Forced pointing People
  name:"panda",
  age:25,
  dothings:function(){
   return this.name+this.age;
  }
}
var people = new People();
alert(people.constructor); //function People(){}

Innovate objects literally, using the constructor attribute not to point to the instance, but to Object.

Every function created has an prototype (stereotype) property, that is, an object. The purpose of the stereotype is to contain properties and methods shared by all instances of a specific type. In other words, it is the prototype object of the object that prototype created by calling the constructor.

Benefits of a prototype object: Let all object instances share its contained properties and methods. Instead of defining object information in the constructor, you can add these directly to the prototype.


// Declaration 1 Constructor 
 function People(){}
 // Adding Attributes to Prototypes 
 People.prototype.name="panda";
 People.prototype.age=25;
 people.prototype.dothings=function(){
  return this.name+this.age;
 }  
 // Instantiate constructor 
 var people1 = new People();
 var people2 = new People();
 alert(people1.name==perople2.name) //true  Because they are all methods in prototypes, their addresses are shared, and everyone comes from the same 1 Place. 

The prototype cannot be accessed using the object instance, the prototype attribute is an object, and the prototype object needs to be accessed through people.__proto__ or the constructor name (object name) People. prototype, but this attribute is not supported in IE browser (undefind), and __proto__ is a pointer to the prototype object.

In addition, there is an constructor attribute, which is a construction attribute. The function of obtaining the constructor itself is: positioned by the prototype pointer, and the constructor itself is obtained, that is, the function of the object instance corresponding to the prototype object.

To interpret whether an object points to the prototype object of the constructor, you can use the isPrototypeOf method to test

People.prototype.isPrototypeOf(people1)  //true

If the same attributes exist in both the instance and the prototype, such as the name attribute under People in the example,


var people = new People();
people.name="bear";
alert(people.name); //bear
alert(people1.name); //panda

Instance attributes do not re-prototype attributes, and the objects of instances adopt the proximity principle.


Related articles: