Created
January 27, 2014 13:09
-
-
Save rssilva/8648325 to your computer and use it in GitHub Desktop.
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
var Human = function (name, age) { | |
return { | |
init: function () { | |
this.setName(name); | |
this.setAge(age); | |
}, | |
setName: function (name) { | |
this.setName = name; | |
}, | |
setAge: function (age) { | |
this.setAge = age; | |
} | |
} | |
} | |
var einstein = new Human('Albert Einstein', 40); | |
einstein.init(); | |
Human.getName = function () { | |
return this.name; | |
} | |
Human.prototype.getAge = function () { | |
return this.age; | |
} | |
console.log(einstein.getName()); //throws an error | |
console.log(einstein.getAge()); //throws an error | |
//////////////////////////////////////////////// | |
var Nazgul = function (name, age) { | |
this.init(name, age); | |
} | |
Nazgul.prototype.init = function (name, age) { | |
this.setName(name); | |
this.setAge(age); | |
} | |
Nazgul.prototype.setName = function (name) { | |
this.name = name; | |
} | |
Nazgul.prototype.setAge = function (age) { | |
this.age = age; | |
} | |
var witchKing = new Nazgul('Witch King', 500); | |
Nazgul.prototype.getName = function () { | |
return this.name; | |
} | |
console.log(witchKing.getName()); //Witch King |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment