Created
March 7, 2010 20:27
-
-
Save mikeyk/324606 to your computer and use it in GitHub Desktop.
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 Post(){}; | |
Post.prototype = {test : function(){console.log("hello there")}}; | |
p = new Post(); | |
p.test(); | |
/* but I prefer to do two things differently: define my member functions in the 'class' function, and have objects inherit from objects: */ | |
function Post() { | |
this.test = function(){ console.log("hello there") } | |
} | |
var post = (function(){ function F(){}; F.prototype = new Post(); return new F()})(); | |
/* or generically: */ | |
function Create(base) { function F(){}; F.prototype = new base(); return new F()} | |
var post = Create(Post); | |
post.test(); // -> 'hello there' | |
/* and to inherit: */ | |
function SubPost(){ this.test = function(){console.log("overriden!")} }; | |
var post2 = Create(SubPost); | |
post2.test(); // -> 'overriden!' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment