You do not know the JS series (42) - JS class mechanism

 

The concept of "class" and "instance" is derived from housing construction.

Just blueprints building plans, they are not really building, we also need construction workers to build the building. Construction workers building will be constructed according to the blueprint. In fact, he planned properties will be copied from the blueprint to building real world

Building became the blueprint for the physical instance, is essentially a copy of the blueprint.

If you want to open a door, it must be in contact with the real job of building - a blueprint which can only indicate a door should be, but is not a real door.

Class is just an abstract representation, describes the need to do something, you must first instantiate the class before you can operate it

Class instance by a method of constructing a special class, this method generally named classes, the constructor is called. All the information of this method is to initialize the task instance ah required (following pseudo-code)
class CoolGuy {
  specialTrick = nothig
 
  CoolGuy(trick) {
    specialTrick = trick
  }

  showOff () {
    console.log('Here is my trick: ', this.specialTrick)
  }

}
var Joe = new CoolGuy('jumping');
Joe.showOff();

CoolGuy class has a CoolGuy () constructor, in fact, is called upon to perform new CoolGuy () it. Constructor returns an object that is an instance of class, then we can call showOff () method on the object to output the designated specialty.


It belongs to the class class constructor, but usually named classes. Most new constructor needs to adjust, so that the engine did not know the language you want to construct a new instance of the class.

In es6 syntax collectively denoted by the constructor of the class constructor
class CoolGuy {
  constructor(trick) {
    this.specialTrick = trick
  }

  showOff () {
    console.log('Here is my trick: ', this.specialTrick)
  }

}
var Joe = new CoolGuy('jumping');
Joe.showOff()

 

 

Guess you like

Origin www.cnblogs.com/wzndkj/p/12624249.html