es6新增的面向对象class类
es5继承使用的是prototype
function Car(option) {
return this.title = option.title;
}
Car.prototype.drive = function () {
return "boom";
}
const car = new Car({ title: "bwm" });
console.log(car.title);
console.log(car.drive());
function toyoto(option) {
Car.call(this, option);
this.color = option.color;
}
toyoto.prototype = Object.create(Car.prototype);
toyoto.prototype.constructor = toyoto;
const toyotos = new toyoto({ color: "green", title: "lalala" });
console.log(toyotos.color);
console.log(toyotos.title);
console.log(toyotos.drive());
es6新增class类的用法
class Car {
constructor({ title }) {
this.title = title
}
drive() {
return "boom"
}
}
var car = new Car({ title: "bwm" });
console.log(car);
console.log(car.title);
class toyoto extends Car {
constructor(options) {
super(options);
this.color = options.color;
}
}
var title = new toyoto({ color: "green", title: "lalala" });
console.log(title);
console.log(title.color);
console.log(title.title);