标签:room drive 实例化 red rom pre class extend ext
/* ES5使用class 继承 */ //方法对象 function Car(options){ this.title = options.title; } //开车 Car.prototype.drive = function(){ return "vromm" }; //实例化 const car1 = new Car({title:"事实上"}); //继承 function Toyota(options){ this.color = options.color; Car.call(this,options);///重要 } Toyota.prototype = Object.create(Car.prototype); Toyota.prototype.constructor = Toyota;//Toyota 拥有 drive方法 const toyota = new Toyota({color:‘red‘,title:"focus"}); toyota.drive(); /*es6 class 继承 可以用于封装 */ class Car { constructor({title}) {//结构 this.title= title; } drive(){ return "vroom"; } } const car = new Car({title:"bmw"}) class Toyota extends Car{//extends Car 是继承 constructor(options) { super(options);//继承 this.color = options.color; } } const toyota = new Toyota({color:"red",title:"ficus"});
标签:room drive 实例化 red rom pre class extend ext
原文地址:https://www.cnblogs.com/shaozhu520/p/11299162.html