Skip to content

Instantly share code, notes, and snippets.

@sofish
Created January 2, 2012 04:44
Show Gist options
  • Save sofish/1549380 to your computer and use it in GitHub Desktop.
Save sofish/1549380 to your computer and use it in GitHub Desktop.
在 js 实例化的时候去 new
function Call(name){
if(!(this instanceof Call)) return new Call(name);
this.name = name;
this.hello = function(){
alert('hello, ' + this.name);
}
}
var hi = Call('sofish'); // 有没有 new 都无所谓
alert(hi.name);
hi.hello();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment