Created
June 4, 2012 14:40
-
-
Save sud0n1m/2868807 to your computer and use it in GitHub Desktop.
Functional code...
This file contains hidden or 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 Penguin(name) { | |
this.name = name; | |
this.numLegs = 2; | |
} | |
// create your Emperor class here and make it inherit from Penguin | |
function Emperor(name){ | |
this.prototype = new Penguin(name); | |
} | |
var emperor = new Emperor("ralph"); | |
// create an "emperor" object and print the number of legs it has | |
console.log(emperor.numLegs); |
This file contains hidden or 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 Penguin(name) { | |
this.name = name; | |
this.numLegs = 2; | |
} | |
// create your Emperor class here and make it inherit from Penguin | |
function Emperor(name){ | |
} | |
Emperor.prototype = new Penguin(name); | |
var emperor = new Emperor("ralph"); | |
// create an "emperor" object and print the number of legs it has | |
console.log(emperor.numLegs); |
You're wanting to set the prototype of the Emperor "class". Inside the definition of the class (your "doesn't work" example), the Emperor class isn't yet defined, so you're most likely setting the prototype for some other object? The current context?
If you console.log(this)
on line 9 of the "doesn't work" example, you'll find out what you're referencing.
Javascript "classes" aren't that great. Almost every JS framework creates their own inheritance structure. Good to know, but they're more of a hack then true inheritance that you get in other languages.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
John, I'm curious as to why I can't define "this.prototype" in the class declaration. And I have to do it outside? Is it because I'm using prototype?