* 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>
1.5 KiB
1.5 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7dad367417b2b2512b75 | 在对象上创建方法 | 1 | 301318 | create-a-method-on-an-object |
--description--
对象
可以有一个叫做方法
的特殊属性
。
方法
其实是一个值为函数的属性
,它可以为一个对象
添加不同的行为。以下就是一个带有方法属性的duck
示例:
let duck = {
name: "Aflac",
numLegs: 2,
sayName: function() {return "The name of this duck is " + duck.name + ".";}
};
duck.sayName();
// Returns "The name of this duck is Aflac."
这个例子给duck
对象添加了一个sayName 方法
,这个方法返回一个包含duck
名字的句子。 注意:这个方法
在返回语句中使用duck.name
的方式来获取name
的属性值。在下一个挑战中我们将会使用另外一种方法来实现。
--instructions--
给dog 对象
设置一个名为sayLegs
的方法,并让它返回 "This dog has 4 legs." 这句话。
--hints--
dog.sayLegs()
应该是一个函数。
assert(typeof dog.sayLegs === 'function');
dog.sayLegs()
应该返回给定的字符串,需要注意标点和间距的问题。
assert(dog.sayLegs() === 'This dog has 4 legs.');
--seed--
--seed-contents--
let dog = {
name: "Spot",
numLegs: 4,
};
dog.sayLegs();
--solutions--
let dog = {
name: "Spot",
numLegs: 4,
sayLegs () {
return 'This dog has ' + this.numLegs + ' legs.';
}
};
dog.sayLegs();