Skip to content

Instantly share code, notes, and snippets.

@bencooling
Created March 13, 2015 11:18
Show Gist options
  • Save bencooling/8d783f6dee4b9e93d35c to your computer and use it in GitHub Desktop.
Save bencooling/8d783f6dee4b9e93d35c to your computer and use it in GitHub Desktop.
prototype examples
// method on prototype
function Foo(){}
Foo.prototype.me = function(){ return "Foo"; };
function Bah(){}
Bah.prototype = new Foo();
Bah.prototype.me = function(){
return Object.getPrototypeOf(Object.getPrototypeOf(this)).me() + ' & Bah';
};
new Bah().me();
// "Foo & Bah"
// method within constructor (child class wont need to "share" methods so
// use within constructor)
function Foo(){}
Foo.prototype.me = function(){ return "Foo"; };
function Bah(){
this.me = function(){
return Object.getPrototypeOf(this).me() + ' & Bah';
};
}
Bah.prototype = new Foo();
new Bah().me();
// "Foo & Bah"
// Same as above
function Bah(){
this.me = function(){
return Object.getPrototypeOf(this).me() + ' & Bah';
};
}
Bah.prototype = {
me: function(){ return "Foo"; }
}
new Bah().me();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment