Skip to content

Instantly share code, notes, and snippets.

@mikeyk
Created March 7, 2010 20:27
Show Gist options
  • Save mikeyk/324606 to your computer and use it in GitHub Desktop.
Save mikeyk/324606 to your computer and use it in GitHub Desktop.
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