Skip to content

Instantly share code, notes, and snippets.

@clarkf
Created November 12, 2010 00:08
Show Gist options
  • Save clarkf/673480 to your computer and use it in GitHub Desktop.
Save clarkf/673480 to your computer and use it in GitHub Desktop.
function Person(name, age, height, weight) {
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
}
Person.prototype.speak = function(speech) {
console.log(this.name+": "+speech);
}
Person.prototype.jump = function() {
this.height += 5;
}
Ninja = Object.create(Person);
Ninja.prototype.constructor = function (name, height, weight, beltColor) {
this.beltColor = beltColor;
Person.call(this, name, "infinity", height, weight); //Call the super constructor...
}
//Overload a function
Ninja.prototype.jump = function() {
this.height += 15;
}
//Then, instantiate:
var p = new Person("John Doe",70,6.1,170);
p.speak("Hello world!");
//"John Doe: Hello world!"
p.jump();
alert(p.height);
//21.1
var n = new Ninja("Ninja Joe",6.2,170,'yellow');
//Uncaught TypeError: object is not a function
n.speak("Hiyah!");
n.jump();
alert(n.height);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment