Created
August 2, 2014 19:32
-
-
Save pedromcunha/591d1ed814c3f0d562a1 to your computer and use it in GitHub Desktop.
OOP Javascript: Inheritance using constructors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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