Created
August 22, 2010 21:01
-
-
Save huseyinyilmaz/544277 to your computer and use it in GitHub Desktop.
Javascript prototypal inheritance samples
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
/* | |
Sets prototype of a object first way | |
*/ | |
var a = {afunc:function(){alert("a");}}; | |
var b = {bfunc:function(){alert("b");}}; | |
a.__proto__ = b; | |
a.bfunc(); | |
/* | |
Same thing with constructors. | |
*/ | |
var A = function(){ //This will be our constructors A = A.prototype.constructor | |
this.afunc = function(){ | |
alert("a"); | |
} | |
} | |
var b = { //this will be prototypes of our objects | |
bfunc : function(){ | |
alert("b"); | |
} | |
} | |
A.prototype = b; | |
var a = new A(); | |
a.bfunc(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment