8.4类
- 类的两种定义方式 变量声明 和 表达式。
- 函数声明可以提升,但类声明不可以提升。
- 另一个跟函数声明不同的地方是,函数受函数作用域限制,而类受块作用域限制。
constructor
方法名 constructor会告诉解释器在使用new 操作符创建类的实例时,应该调用这个函数。类实例化传入的参数会当做构造函数的参数。默认情况下,constructor 构造函数会默认返回this 对象 。 调用类构造函数必须使用 new 操作符,否则会报错。

静态成员 static ,常用于执行不特定实例的操作。 返回的this引用类自身。
class Person {*getElement(){yield 'a';yield 'b';yield 'c';}}let e = new Person();let v = e.getElement()console.log(v.next().value); //aconsole.log(v.next().value); //bconsole.log(v.next().value); //c
class Vehical{identifyP(id){console.log(id,this)}static indentifyC(id){console.log(id, this);}}class Car extends Vehical {};let myVehical = new Vehical();let myCar = new Car();myVehical.identifyP("AUTO")myCar.identifyP("BMW")Car.indentifyC("BMW1")
super
派生类的方法可以通过 super 关键字引用它们的原型
super只能在派生类构造函数和静态方法中使用- 不能单独使用
super关键字。要么用它调用构造函数,要么用它引用静态方法 - 调用
super()会调用父级构造函数,并将返回的实例赋给this super()的行为如同调用构造函数,如果需要给父类构造函数传参,则需要手动传入- 如果没有定义类构造函数,在实例化派生类时会调用
super(),而且会传入所有传给派生类的参数。 - 在类构造函数中,不能在
super之前引用this - 如果在派生类中显式定义了构造函数,则一定要调用
super或者返回一个对象。
