Simple inheritance in ECMAScript 5

February 26th, 2013 § 1 comment § permalink

Here is the simple inheritance in Javascript (ECMAScript 5).



 var person = Object.create(null);
 Object.defineProperty(person,"name", {value:"Revath"});
 console.log(person.name);
 Object.defineProperty(person, "toString", {value: function(){
 	return this.name + " attended " + this.school;
 }});
 
 Object.defineProperties(person, {
 	school:{
 		value:"st. Joseph's BHSS"
 	},
 	college:{
 		value: "NTTF"
 	}
 });
console.log(person.school);
console.log(person.college);
 
var driver = Object.create(person);
Object.defineProperty(driver,"canDrive", {value: ['bike', 'car']});
 
console.log(driver.name);
console.log(driver.school);
console.log(driver.canDrive);
console.log(driver.toString());