--- id: 587d7dad367417b2b2512b77 challengeType: 1 forumTopicId: 16804 title: 定义构造函数 --- ## Description
构造函数用以创建一个新对象,并给这个新对象定义属性和行为。因此这是创建新对象的一个最基本的方式。 以下就是一个构造函数的示例: ```js function Bird() { this.name = "Albert"; this.color = "blue"; this.numLegs = 2; } ``` 这个构造函数定义了一个Bird对象,其属性namecolornumLegs的值分别被设置为Albertblue和 2。 构造函数遵循一些惯例规则:
## Instructions
创建一个构造函数Dog。给其添加namecolornumLegs属性并分别给它们设置为:字符串,字符串和数字。
## Tests
```yml tests: - text: Dog应该有一个name属性且它的值是一个字符串。 testString: assert(typeof (new Dog()).name === 'string'); - text: Dog应该有一个color属性且它的值是一个字符串。 testString: assert(typeof (new Dog()).color === 'string'); - text: Dog应该有一个numLegs属性且它的值是一个数字。 testString: assert(typeof (new Dog()).numLegs === 'number'); ```
## Challenge Seed
```js ```
## Solution
```js function Dog (name, color, numLegs) { this.name = 'name'; this.color = 'color'; this.numLegs = 4; } ```