Skip to content

Instantly share code, notes, and snippets.

@send2moran
Created August 12, 2014 08:12
Show Gist options
  • Save send2moran/6b1660ec83310e8ec94b to your computer and use it in GitHub Desktop.
Save send2moran/6b1660ec83310e8ec94b to your computer and use it in GitHub Desktop.
We need to make sure that the new operator is always used.
function User(first, last){
if ( !(this instanceof User) )
return new User(first, last);
this.name = first + " " + last;
}
var name = "Resig";
var user = User("John", name);
assert( user, "This was defined correctly, even if it was by mistake." );
assert( name == "Resig", "The right name was maintained." );
@send2moran
Copy link
Author

function User(first, last){
if ( !(this instanceof arguments.callee) )
return new User(first, last);

this.name = first + " " + last;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment