コンストラクタの括弧は省略が可能


コンストラクタに引数をとらない場合は、new Hoge(); の () を省略し、new Hoge; と書くことができる。これは知らなかった。つまり、コンストラクタの括弧は、関数呼び出しのための括弧ではなくて、引数を渡すための括弧なのである。JavaScript The Definitive Guide を読み返したら、ちゃんと省略している例も書いてあった。

そして、仕様を当たってみる。

ECMAScript Language Specification

ECMAScript Language Specificationの、11.2.2 The new Operator 部分。

The production NewExpression : new NewExpression is evaluated as follows:…

引数がない場合と、

The production MemberExpression : new MemberExpression Arguments is evaluated as follows:…

引数がある場合の評価方法が書かれている。

MSDN

MSDNの new Operator 項を当たってみると

The parentheses can be omitted if the constructor takes no arguments.

とある。

カテゴリー: Flash   パーマリンク

コメントをどうぞ

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>