コンストラクタ関数 Dog に () をつけていない次のコードが問題なく動きます。JavaScript の new Dog は new Dog() と何か違うのでしょうか。
function Dog() {this.say = function () {console.log("wan");};}
d = new Dog;
d.say(); //=> wan以下の資料に載っているのかもしれないのですが、読み取れませんでした。
コンストラクタ関数 Dog に () をつけていない次のコードが問題なく動きます。JavaScript の new Dog は new Dog() と何か違うのでしょうか。
function Dog() {this.say = function () {console.log("wan");};}
d = new Dog;
d.say(); //=> wan以下の資料に載っているのかもしれないのですが、読み取れませんでした。
同じです。
MDN の JavaScript リファレンス から引用(強調は引用者):
The constructor function foo is called with the specified arguments and this bound to the newly created object.
new foois equivalent tonew foo(), i.e. if no argument list is specified, foo is called without arguments.
この質問の答えをご存知ですか? ぜひ質問者に伝えてあげてください (サインインすると、回答フォームが表示されます)。
サインイン