Skip to content

Instantly share code, notes, and snippets.

@huseyinyilmaz
Created August 22, 2010 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huseyinyilmaz/544277 to your computer and use it in GitHub Desktop.
Save huseyinyilmaz/544277 to your computer and use it in GitHub Desktop.
Javascript prototypal inheritance samples
/*
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