Inheritance and encapsulation ES5 es6

/ * 
The ES5 using inheritance class 
* / 
// method of the object 
function Car (Options) { 
	this.title = options.title; 
} 

// drive 
Car.prototype.drive = function () { 
	return "vromm" 
}; 

// instantiate 
const car1 = new Car ({title : " in fact"}); 


// inherit 
function Toyota (Options) { 
	this.color = options.color; 
	Car.call (the this, Options); /// important 
	
} 

Toyota.prototype the Object.create = (Car.prototype); 
Toyota.prototype.constructor = Toyota; // Toyota drive method have 


const = new new Toyota Toyota ({Color: 'Red', title: "Focus"}); 
toyota.drive () ; 



/ * class inheritance can be used to encapsulate ES6 * / 
class Car { 
	 
	constructor (title {}) {// structure
		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"});

  



 

Guess you like

Origin www.cnblogs.com/shaozhu520/p/11299162.html