JS: creating subclasses and managing prototypes

 

function Job(pays){
    this.pays = pays;
}

Job.prototype.print = function(){
    console.log( this.pays ? "Please hire me" : "I am not interested");
};

function TechJob(title, pays){
    Job.call(this, pays);
    
    this.title = title;
}

// this is creating a new empty object and setting its prototype to Job.prototype 
TechJob.prototype = Object.create( Job.prototype );
TechJob.prototype.constructor = TechJob;


// we are adding a print method to TechJob.prototype, so it wont go any further to find print method.
TechJob.prototype.print = function(){
    console.log( this.pays ? "I'm in" : "I'm out");
};

var x1 = new TechJob("web developer", true);
x1.print();

var x2 = new Job(true);
x2.print();

console.dir(x1);

2016-06-11 15_44_25-TODO supply a title

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *