Bird
构造函数: function Bird(){请注意,在调用构造函数时使用
this.name =“阿尔伯特”;
this.color =“blue”;
this.numLegs = 2;
//构造函数中的“this”始终引用正在创建的对象
}
让blueBird = new Bird();
new
运算符。这告诉JavaScript创建一个名为blueBird
的Bird
新instance
。如果没有new
运营商, this
在构造函数中不会指向新创建的对象,给人意想不到的效果。现在, blueBird
具有在Bird
构造函数中定义的所有属性: blueBird.name; // =>艾伯特就像任何其他对象一样,可以访问和修改其属性:
blueBird.color; // =>蓝色
blueBird.numLegs; // => 2
blueBird.name ='Elvira';
blueBird.name; // =>埃尔维拉
Dog
构造函数创建Dog
的新实例,将其分配给变量hound
。 Dog
构造函数创建hound
。
testString: assert(hound instanceof Dog);
- text: 您的代码应该使用new
运算符来创建Dog
的instance
。
testString: assert(code.match(/new/g));
```