Created
July 8, 2012 20:48
-
-
Save eduardocereto/3072731 to your computer and use it in GitHub Desktop.
Simple Class Instantiation Javascript
This file contains hidden or 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
var User = makeClass(); | |
User.prototype.init = function(first, last){ | |
this.name = first + " " + last; | |
}; | |
var user = User("John", "Resig"); | |
user.name | |
// => "John Resig" | |
This file contains hidden or 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
// http://ejohn.org/blog/simple-class-instantiation/ | |
// makeClass - By John Resig (MIT Licensed) | |
function makeClass(){ | |
return function(args){ | |
if ( this instanceof arguments.callee ) { | |
if ( typeof this.init == "function" ) | |
this.init.apply( this, args.callee ? args : arguments ); | |
} else | |
return new arguments.callee( arguments ); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment