Created
March 13, 2015 11:18
-
-
Save bencooling/8d783f6dee4b9e93d35c to your computer and use it in GitHub Desktop.
prototype examples
This file contains 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
// 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