*class语法相对原型、构造函数、继承更接近传统语法,它的写法能够让对象原型的写法更加清晰、面向对象编程的语法更加通俗
这是class的具体用法
例子:
class Animal {
constructor() {
this.type = 'animal' ;
}
say(say) {
console.log( this.type + ' says ' +say ) ;
}
}
let animal = new Animal() ;
animal.say( 'hello' ) ; // animal says hello
class Dog extends Animal {
constructor() {
super() ;
this.type = 'dog' ;
}
}
let dog = new Dog() ;
dog.say( 'hot' ) ; // dog says hot
class Cat extends Animal {
constructor() {
super();
this.type = 'cat';
}
}
let cat = new Cat();
cat.says('cool'); // cat says cool