Skip to content

Instantly share code, notes, and snippets.

@pedromcunha
Created August 2, 2014 19:32
Show Gist options
  • Save pedromcunha/591d1ed814c3f0d562a1 to your computer and use it in GitHub Desktop.
Save pedromcunha/591d1ed814c3f0d562a1 to your computer and use it in GitHub Desktop.
OOP Javascript: Inheritance using constructors
function Tool(name, type, use, cost) {
this.name = name;
this.type = type;
this.use = use;
this.cost = cost;
//You can also add inherited functions here for everything created using this constructor
this.useTool = function () {
alert("Alright, be careful with that "+this.name+"!");
}
}
//Important thing to note: constructors are usually capitalized at the beginning. E.g. Tools
var hammer = new Tool("hammer", "manual", "To Nail things", 5);
console.log(hammer);
hammer.useTool();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment