* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2.7 KiB
2.7 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7dae367417b2b2512b79 | 扩展构造函数以接收参数 | 1 | 18235 | extend-constructors-to-receive-arguments |
--description--
上一个挑战中Bird
和Dog
构造函数运行得不错。但是,注意到没有:所有通过Bird
构造函数创建出来的实例Birds
都自动的取名为 Albert,颜色都是蓝色,还都有两条腿。如果你想要新创建出来的小鸟们拥有不同的名字和颜色要怎么办呢?当然,手动的去修改每一个小鸟实例自己的属性也是可以实现的,只是会增加很多无谓的工作量:
let swan = new Bird();
swan.name = "Carlos";
swan.color = "white";
假如你写了一个程序来追踪一个鸟舍里面的几百只甚至几千只不同的小鸟,你将会花费很多时间去创建所有的小鸟实例并给它们的属性一一修改为不同的值。 为了减轻创建不同Bird
对象的工作量,你可以给你的Bird
设置为可以接收参数的构造函数:
function Bird(name, color) {
this.name = name;
this.color = color;
this.numLegs = 2;
}
然后将值通过参数的方式传递给Bird
构造函数来定义每一个唯一的小鸟实例: let cardinal = new Bird("Bruce", "red");
这给Bird
的名字和颜色属性分别赋值为 Bruce 和红色提供了另外一种方法。但numLegs
属性被默认赋值为 2。 cardinal
有以下这些属性:
cardinal.name // => Bruce
cardinal.color // => red
cardinal.numLegs // => 2
这样一来构造函数就变得很灵活了。现在可以直接定义每个Bird
实例在创建时的属性,这是 JavaScript 构造函数非常实用的用法之一。它们根据共同或相似的属性和行为将对象归纳为一组,并能够自动的创建各自实例。
--instructions--
创建另一个Dog
构造函数。这一次,给它设置两个参数:name
和color
,同时给numLegs
赋值为 4。然后创建一个新Dog
实例保存为变量名:terrier
,再将两个字符串通过参数的形式传入name
和color
属性。
--hints--
Dog
应该接收一个name
参数。
assert(new Dog('Clifford').name === 'Clifford');
Dog
应该接收一个color
参数。
assert(new Dog('Clifford', 'yellow').color === 'yellow');
Dog
应该有一个numLegs
属性且值为 4。
assert(new Dog('Clifford').numLegs === 4);
terrier
应该是通过Dog
构造函数创建的。
assert(terrier instanceof Dog);
--seed--
--seed-contents--
function Dog() {
}
--solutions--
function Dog (name, color) {
this.numLegs = 4;
this.name = name;
this.color = color;
}
const terrier = new Dog();