//creating a function expression which is always a constructor
let x = function(j) {
this.i = 0;
this.j = j;
}
// i want to take every property of the x method and extend it to getjmethod with prototype
x.prototype.getJ = function() {
return this.j;
}
//i create two instances of x method
let x1 = new x(1);
let x2 = new x(2);
//i call the getJ method on the instances to get the value
console.log(x1.getJ())
console.log(x2.getJ())
// More examples
let Job = function(){
this.pays = true
}
Job.prototype.print = function(){
console.log(this.pays ? 'I want to join' : 'no thanks');
}
//creating a sub class
let Techjob = function(title, pays){
Job.call(this); // inherits the properties of the master object
this.title = '';
this.pays = pays;
}
// create instance of the subclass
Techjob.prototype = Object.create(Job.prototype);
Techjob.prototype.constructor = Techjob;
let softwareJob = new Techjob('js', false);
console.log(softwareJob.print())
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.