Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* 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>
2021-01-12 19:31:00 -07:00

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();