Last active
June 1, 2017 04:04
-
-
Save srinivasKandukuri/35e9aecb4e48259ff919816a743ea21c to your computer and use it in GitHub Desktop.
Javascript Basic inheritance example
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
| var Human = function(name){ | |
| this.name = name; | |
| }; | |
| Human.prototype.talk = function(){ | |
| return 'this is Human talking'; | |
| } | |
| Human.prototype.walk = function(){ | |
| return 'I am Human I can walk with legs' | |
| } | |
| var Animal = function (){}; | |
| Animal.prototype.talk = function(){ | |
| return 'this is Animal talking'; | |
| } | |
| function Ben(){ | |
| Human.call(this, 'BEN'); | |
| Animal.call(this); | |
| } | |
| Ben.prototype.sleep = function(human){ | |
| return human.name; | |
| } | |
| Ben.prototype.talk= function(){ | |
| return Human.prototype.talk.call(this); | |
| } | |
| Ben.prototype = $.extend({}, Human.prototype, Animal.prototype, Ben.prototype); | |
| var foo = new Ben(); | |
| foo.talk(); |
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
| function Employee(name){ | |
| this.name = name; | |
| this.dept = 'IT'; | |
| } | |
| Employee.prototype.details = function(name,dept){ | |
| this.name = name; | |
| this.dept = dept; | |
| console.log('Employee Name : '+this.name +' And His Dept : '+ this.dept); | |
| } | |
| function Developer(){ | |
| Employee.call(this, 'JsNinja'); | |
| } | |
| Developer.prototype = Object.create(Employee.prototype); | |
| Developer.prototype.constructor = Developer; | |
| var dev1 = new Developer(); | |
| console.log(dev1.details('srinivas', 'Devopps')); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment