Created
July 19, 2012 12:50
-
-
Save nsisodiya/3143607 to your computer and use it in GitHub Desktop.
JS-Inheritance-5.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Author : Narendra Sisodiya | |
* Date : Thu Jul 19 18:16:38 IST 2012 | |
* Licence : Public Code | |
* Purpose : Inheritance | |
* | |
*/ | |
Function.prototype.inheritFrom = function (Base){ | |
var F = function(){}; | |
F.prototype = Base.prototype; | |
var old_prototype = this.prototype; | |
this.prototype = new F(); | |
this.prototype.constructor = this; | |
for (i in old_prototype) { | |
this.prototype[i] = old_prototype[i]; | |
} | |
this.prototype._super = function (){ | |
if(arguments.length >= 1 ){ | |
Base.apply(this, arguments); | |
}else{ | |
Base.call(this); | |
} | |
} | |
} | |
var Person = function(full_name, age) { | |
this.full_name = full_name; | |
this.age = age; | |
} | |
Person.prototype = { | |
drive: function() { | |
if(this.age >= 18){ | |
alert(this.full_name + " is driving"); | |
}else{ | |
alert(this.full_name + " is not eligible for driving"); | |
} | |
return this; | |
}, | |
eat: function(){ | |
alert(this.full_name + " is eating"); | |
return this; | |
} | |
}; | |
var Employee = function(full_name, age, job_title){ | |
this._super(full_name, age); | |
this.job_title = job_title; | |
}; | |
Employee.prototype = { | |
office: function(){ | |
alert(this.full_name + " is a "+ this.job_title + " and he is going to Office"); | |
return this; | |
} | |
}; | |
Employee.inheritFrom(Person); | |
var narendra = new Employee("Narendra", 30, "Engineer"); | |
narendra.eat().drive().office(); | |
var chetan = new Employee("Chetan", 17, "Doctor"); | |
chetan.eat().drive().office(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment