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, "hound should be created using the Dog constructor.");'
- text: 您的代码应该使用new运算符来创建Dog的instance 。
testString: 'assert(code.match(/new/g), "Your code should use the new operator to create an instance of Dog.");'
```