ES6 class

A class is essentially just a syntactic sugar, with the underlying functions created through constructors. Contains constructor, properties, methods, etc.

// Class Studet {constructor(name, age) {// This points to the object/instance of the current constructor console.log(this); // attribute this.name = name this.age = age} // method say() {console.log(' name ${this.name}, age ${this.age} '); } // New Studet("zhangsan", 20)Copy the code

ES6 Class inheritance The class inheritance contains extend and super, extending property methods

Class People {constructor(name, age) {// This points to the object/instance of the current constructor console.log(this); // attribute this.name = name} // method eat() {console.log(' name ${this.name} eat '); }} // subclass Studet extends People {constructor(constructor) Age) {super(name) this.age = age} say() {console.log(' name ${this.name}, age ${this.age} '); }} class Teacher extends People {constructor(name, constructor) Major) {super(name) this.major = Major} teach() {console.log(' name ${this.name} teach ${this.major} '); }} const zhangsan = new Studet("zhangsan", 20) zhangsan.eat() zhangsan.say() const lisi = new Teacher("lisi", "language ") lisi.teach() lisi.say()// errorCopy the code

Key points:

    lisi instanceof Teacher  //true
    lisi instanceof People   //true
    lisi instanceof Object   //true
Copy the code

The prototype

  • Each class has an explicit prototype prototype
  • Each instance has an implicit prototype __protp__
  • The instance’s __protp__ points to the class’s prototype
Prototype //People //lisi display prototype is equal to Teacher display lisi ===Teacher.prototype //trueCopy the code

Prototype chain

Picture from the network, deleted infringement

Teacher.prototype.__protp__ ===People.prototype  //true
Studet.prototype.__protp__ ===People.prototype  //true
Copy the code