diff options
Diffstat (limited to 'inheritance.js')
-rw-r--r-- | inheritance.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/inheritance.js b/inheritance.js new file mode 100644 index 0000000..2fab447 --- /dev/null +++ b/inheritance.js @@ -0,0 +1,29 @@ +function Person(name){ + this.name = name; + this.type = 'human'; + this.info = function() { + console.log("Name:", this.name, "Type:", this.type); + } +} +Person.prototype.roboinfo = function(){ + console.log("Name:", this.name, "Type:", this.type, "Processor:", this.cpu); +} + +function Robot(name){ + Person.call(this, name) // akin of calling super, this is what allows us to inherit props + this.type = 'robot'; + this.cpu = 'AMD64' +} + +// Set Robot's prototype to Person's prototype +Robot.prototype = Person.prototype; + +// Set constructor back to Robot +Robot.prototype.constructor = Robot; + +person = new Person("Bob"); +robot = new Robot("Bender"); + +person.info(); // Name: Bob Type: human +robot.info(); // Name: Bender Type: robot +robot.roboinfo(); // Name: Bender Type: robot Processor: AMD64
\ No newline at end of file |