Skip to content

Instantly share code, notes, and snippets.

@sud0n1m
Created June 4, 2012 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sud0n1m/2868807 to your computer and use it in GitHub Desktop.
Save sud0n1m/2868807 to your computer and use it in GitHub Desktop.
Functional code...
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);
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);
@sud0n1m
Copy link
Author

sud0n1m commented Jun 4, 2012

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?

@jrallison
Copy link

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.

@jrallison
Copy link

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