LoginSignup
4

More than 5 years have passed since last update.

newのつけ忘れに対応する書き方

Last updated at Posted at 2012-08-23

エラー出してあげたほうが良い気もするけど、、こういう書き方もあるんだというメモ。

javascript
function SomeClass(){
    return (this instanceof SomeClass) ? this.init() : new SomeClass();
}

SomeClass.prototype.init = function(){
    console.log('SomeClass.init')
}

var hoge = SomeClass();
// >> SomeClass.init

var fuga = new SomeClass();
// >> SomeClass.init

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4