這在建構函式中
當使用函式作為建構函式時 ,它有一個特殊的 this
繫結,它引用新建立的物件:
function Cat(name) {
this.name = name;
this.sound = "Meow";
}
var cat = new Cat("Tom"); // is a Cat object
cat.sound; // Returns "Meow"
var cat2 = Cat("Tom"); // is undefined -- function got executed in global context
window.name; // "Tom"
cat2.name; // error! cannot access property of undefined