If you want to generate an object instance, you need to define a constructor first, and then complete it through the new operator. Constructor example:
1 //The function name and instantiation construct name are the same and capitalized (not mandatory, but it helps to distinguish between constructors and ordinary functions) 2 function Person(name,age) { 3 this.name = name; 4 this.age=age; 5 } 6 Person.prototype.say = function(){ 7 return "My name is" + this.name+"this year"+this.age+"Years old"; 8 } 9 var obj=new Person("laotie",88);//To create an object through a constructor, you must use new operator 10 console.log(obj.say());//My name is laotie I'm 88 years old
Execution process of constructor generation instance:
- When the constructor is used and the new constructor () is used, the background will implicitly execute new Object() to create the object;
- The scope of the constructor is given to the new object (that is, the object created by new Object()), and this in the function body represents the object created by new Object().
- Execute the code of the constructor.
- Return a new object (returned directly in the background);
ES6 introduces the concept of class, which can be defined through the class keyword. The emergence of this keyword makes it clearer in object writing, and it is more like an object-oriented language. If the previous code is changed to ES6, it will look like this:
1 class Person{//Defines a name Person Class of 2 constructor(name,age){//constructor Is a construction method used to receive parameters 3 this.name = name;//this Represents an instance object 4 this.age=age; 5 } 6 say(){//This is a class method. Be careful not to add it function 7 return "My name is" + this.name+"this year"+this.age+"Years old"; 8 } 9 } 10 var obj=new Person("laotie",88); 11 console.log(obj.say());//My name is laotie I'm 88 years old
Note:
1. When declaring a method in a class, never add the function keyword to the method 2. Methods should not be separated by commas, otherwise an error will be reported
As can be seen from the following code, a class is essentially a function. The class itself points to the constructor. So we can think that the class in ES6 is actually another way to write the constructor!
1 console.log(typeof Person);//function 2 console.log(Person===Person.prototype.constructor);//true
All methods of the class are defined on the prototype attribute of the class
1 Person.prototype.say=function(){//Defines a method with the same name as in the class. Successfully achieved coverage! 2 return "I'm here to prove it. What's your name" + this.name+"this year"+this.age+"Years old"; 3 } 4 var obj=new Person("laotie",88); 5 console.log(obj.say());//I'm here to prove it. What's your name laotie I'm 88 years old
Of course, you can also add methods to the class through the prototype attribute
1 Person.prototype.addFn=function(){ 2 return "I passed prototype Newly added method,Name is addFn"; 3 } 4 var obj=new Person("laotie",88); 5 console.log(obj.addFn());//I passed prototype Newly added method,Name is addFn
You can also use object Assign method to dynamically add methods to the object
1 Object.assign(Person.prototype,{ 2 getName:function(){ 3 return this.name; 4 }, 5 getAge:function(){ 6 return this.age; 7 } 8 }) 9 var obj=new Person("laotie",88); 10 console.log(obj.getName());//laotie 11 console.log(obj.getAge());//88
The constructor method is the default method of the constructor of the class. It is called automatically when an object instance is generated by the new command.
1 class Box{ 2 constructor(){ 3 console.log("Lala, it's sunny today");//This line of code is executed when the object is instantiated. 4 } 5 } 6 var obj=new Box();
If the constructor method is not explicitly defined, it will implicitly generate a constructor method. So even if you don't add a constructor, the constructor still exists. The constructor method returns the instance object this by default, but you can also specify the constructor method to return a new object so that the returned instance object is not an instance of this class.
1 class Box{ 2 constructor(num1,num2){ 3 this.num1 = num1; 4 this.num2=num2; 5 } 6 sum(){ 7 return num1+num2; 8 } 9 } 10 var box=new Box(12,88); 11 console.log(box.hasOwnProperty("num1"));//true 12 console.log(box.hasOwnProperty("num2"));//true 13 console.log(box.hasOwnProperty("sum"));//false 14 console.log("num1" in box);//true 15 console.log("num2" in box);//true 16 console.log("sum" in box);//true 17 console.log("say" in box);//false
All instances of the class share a prototype object, and their prototype is person Prototype, so the proto attribute is equal
1 class Box{ 2 constructor(num1,num2){ 3 this.num1 = num1; 4 this.num2=num2; 5 } 6 sum(){ 7 return num1+num2; 8 } 9 } 10 //box1 And box2 All Box Examples of. Their__proto__All point Box of prototype 11 var box1=new Box(12,88); 12 var box2=new Box(40,60); 13 console.log(box1.__proto__===box2.__proto__);//true
Thus, you can also add methods to classes through proto. Rewriting the prototype with the proto attribute of the instance will change the original definition of Class and affect all instances, so it is not recommended!
1 class Box{ 2 constructor(num1,num2){ 3 this.num1 = num1; 4 this.num2=num2; 5 } 6 sum(){ 7 return num1+num2; 8 } 9 } 10 var box1=new Box(12,88); 11 var box2=new Box(40,60); 12 box1.__proto__.sub=function(){ 13 return this.num2-this.num1; 14 } 15 console.log(box1.sub());//76 16 console.log(box2.sub());//20
Class has no variable promotion, so it needs to be defined before use. Because ES6 does not promote the class declaration to the code header, but ES5 is different. ES5 has variable promotion, which can be used first and then defined.
1 //ES5 You can use it before you define it,Variable promotion exists 2 new A(); 3 function A(){ 4 5 } 6 //ES6 Cannot use before defining,If there is no variable promotion, an error will be reported 7 new B();//B is not defined 8 class B{ 9 10 }