summaryrefslogtreecommitdiffstats
path: root/inheritance.js
blob: 2fab44796c3cae2db632b9cf852b306d04a9b5be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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