在ES6中类的声明和调用
class Car{ constructor({title}){ this.title = title; } drive(){ return 'vroom'; } } const car = new Car({title:"BMW"}); console.log(car); console.log(car.drive());
注意:上面的构造函数传参是以解构形式
在ES6中实现继承
class Toyota extends Car{
constructor(options){
super(options);
this.color = options.color;
}
}
const toyota = new Toyota({color:"red",title:"Focus"});
console.log(toyota);
console.log(toyota.drive());