Happy New Year!

ES5和es6的封装继承

/*
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"});

  



 

posted @ 2019-08-04 18:28  义美-小义  阅读(525)  评论(0编辑  收藏  举报
返回顶部小火箭
世界很公平,想要最好,就一定得付出!
x
github主页