:::info ES6新增的class这种创建对象的方法,从事后端会感觉好棒,但是用起来就那么回事,不会也不耽误写代码,会了可以装个B,基本上算是原来创建对象的提升版本 :::
//正常写法class Animal {constructor(name){ // 构造方法this.name = name}sleep(){return 'zzZZ~'}}let cat = new Animal('cat')let dog = new Animal('dog')console.log(cat.name) // catconsole.log(dog.name) // dogconsole.log(cat.sleep === dog.sleep) // true//继承class Animal {constructor(name){ // 构造方法this.name = name}sleep(){return 'zzZZ~'}}class Flyable extends Animal {constructor(name){super(name) // 执行父类构造方法}fly() {return 'flying...'}}var brid = new Flyable('brid')console.log(brid.name) // bireconsole.log(brid.sleep()) // zzZZ~console.log(brid.fly()) // flying...
