Skip to content

Instantly share code, notes, and snippets.

@reu
Created July 9, 2013 14:37
Show Gist options
  • Save reu/5957848 to your computer and use it in GitHub Desktop.
Save reu/5957848 to your computer and use it in GitHub Desktop.
function inherit(child, parent) {
child.prototype = Object.create(parent.prototype);
child.prototype.constructor = child;
}
var Person = function Person(firstName, lastName) {
if (!firstName || !lastName) throw "Primeiro e último nome requerido!";
this.firstName = firstName;
this.lastName = lastName;
}
Person.prototype.fullName = function() {
return this.firstName + " " + this.lastName;
}
var Ninja = function Ninja(firstName, lastName, stars) {
Person.call(this, firstName, lastName); // super
this.stars = 10;
}
// Note que eu herdei antes de definir o resto das funções
// no prototype do Ninja.
inherit(Ninja, Person);
Ninja.prototype.throwStar = function(first_argument) {
this.stars -= 1;
return "Star throwed";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment