--- id: 587d7dad367417b2b2512b77 title: 定義構造函數 challengeType: 1 forumTopicId: 16804 dashedName: define-a-constructor-function --- # --description-- Constructors 是創建對象的函數。 函數給這個新對象定義屬性和行爲。 可將它們視爲創建的新對象的藍圖。 以下就是一個構造函數的示例: ```js function Bird() { this.name = "Albert"; this.color = "blue"; this.numLegs = 2; } ``` 這個構造函數定義了一個 `Bird` 對象,其屬性 `name`、`color` 和 `numLegs` 的值分別被設置爲 Albert、blue 和 2。 構造函數遵循一些慣例規則: # --instructions-- 創建一個構造函數:`Dog`。 給其添加 `name`,`color` 和 `numLegs` 屬性並分別給它們設置爲:字符串、字符串和數字。 # --hints-- `Dog` 應該有一個 `name` 屬性且它的值是一個字符串。 ```js assert(typeof new Dog().name === 'string'); ``` `Dog` 應該有一個 `color` 屬性且它的值是一個字符串。 ```js assert(typeof new Dog().color === 'string'); ``` `Dog` 應該有一個 `numLegs` 屬性且它的值是一個數字。 ```js assert(typeof new Dog().numLegs === 'number'); ``` # --seed-- ## --seed-contents-- ```js ``` # --solutions-- ```js function Dog (name, color, numLegs) { this.name = 'name'; this.color = 'color'; this.numLegs = 4; } ```