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.8 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7dad367417b2b2512b77 定义构造函数 1 16804 define-a-constructor-function

--description--

构造函数用以创建一个新对象,并给这个新对象定义属性和行为。因此这是创建新对象的一个最基本的方式。

以下就是一个构造函数的示例:

function Bird() {
  this.name = "Albert";
  this.color = "blue";
  this.numLegs = 2;
}

这个构造函数定义了一个Bird对象,其属性namecolornumLegs的值分别被设置为Albertblue和 2。 构造函数遵循一些惯例规则:

  • 构造函数函数名的首字母最好大写,这是为了方便我们区分构造函数和其他非构造函数。
  • 构造函数使用this关键字来给它将创建的这个对象设置新的属性。在构造函数里面,this指向的就是它新创建的这个对象。
  • 构造函数定义了属性和行为就可创建对象,而不是像其他函数一样需要设置返回值。

--instructions--

创建一个构造函数Dog。给其添加namecolornumLegs属性并分别给它们设置为:字符串,字符串和数字。

--hints--

Dog应该有一个name属性且它的值是一个字符串。

assert(typeof new Dog().name === 'string');

Dog应该有一个color属性且它的值是一个字符串。

assert(typeof new Dog().color === 'string');

Dog应该有一个numLegs属性且它的值是一个数字。

assert(typeof new Dog().numLegs === 'number');

--seed--

--seed-contents--

--solutions--

function Dog (name, color, numLegs) {
  this.name = 'name';
  this.color = 'color';
  this.numLegs = 4;
}