* fix: Chinese test suite Add localeTiltes, descriptions, and adjust test text and testStrings to get the automated test suite working. * fix: ran script, updated testStrings and solutions
2.0 KiB
2.0 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7dad367417b2b2512b78 | Use a Constructor to Create Objects | 1 | 使用构造函数创建对象 |
Description
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; // =>埃尔维拉
Instructions
Dog
构造函数创建Dog
的新实例,将其分配给变量hound
。 Tests
tests:
- text: 应该使用<code>Dog</code>构造函数创建<code>hound</code> 。
testString: assert(hound instanceof Dog);
- text: 您的代码应该使用<code>new</code>运算符来创建<code>Dog</code>的<code>instance</code> 。
testString: assert(code.match(/new/g));
Challenge Seed
function Dog() {
this.name = "Rupert";
this.color = "brown";
this.numLegs = 4;
}
// Add your code below this line
Solution
// solution required