Skip to content

Instantly share code, notes, and snippets.

@eduardocereto
Created July 8, 2012 20:48
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 eduardocereto/3072731 to your computer and use it in GitHub Desktop.
Save eduardocereto/3072731 to your computer and use it in GitHub Desktop.
Simple Class Instantiation Javascript
var User = makeClass();
User.prototype.init = function(first, last){
this.name = first + " " + last;
};
var user = User("John", "Resig");
user.name
// => "John Resig"
// 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