您的位置:首页 > 其它

ES6中的面向对象class,对新手学习理解大有帮助。

2018-04-07 09:42 363 查看
//ES5的做法:

//function Car(options){
// this.style = options.style;
//}
//
//Car.prototype.owner = function(){
// return "ivan";
//}
//
//
//const car = new Car({style:"cool"});
//
//console.log(car.style);
//console.log(car.owner());
//
//function BMW(options){
// var self = this;
// Car.call(self,options);
// this.color = options.color;
//}
//
//BMW.prototype = Object.create(Car.prototype);
//BMW.prototype.constructor = BMW;
//
//const bmw = new BMW({color: "red", style: "bigger"});
//
//console.log(bmw);
//console.log(bmw.owner());

//ES6的做法

class Car{
constructor({style}){
this.style = style;
}
owner(){
return "ivan"; 
}
}

//const car = new Car({style : "cool"});
//console.log(car);
//console.log(car.owner());

class BMW extends Car{
constructor(options){
super(options);
this.color = options.color;
}
}

const bmw = new BMW({style:"bigger", color :"red"}) 
console.log(bmw);
console.log(bmw.owner());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: