freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-javascript/build-javascript-objects.ch...

4.0 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
56bbb991ad1ed5201cd392d0 Build JavaScript Objects 1 构建JavaScript对象

Description

您之前可能听说过object这个术语。对象类似于arrays ,除了不使用索引访问和修改数据,您可以通过所谓的properties访问对象中的数据。对象对于以结构化方式存储数据很有用并且可以表示真实世界对象如猫。这是一个示例cat对象
var cat = {
“名字”:“胡须”,
“腿”4
“尾巴”1
“敌人”:[“水”,“狗”]
};
在此示例中,所有属性都存储为字符串,例如 - "name" "legs""tails" 。但是,您也可以使用数字作为属性。您甚至可以省略单字符串属性的引号,如下所示:
var anotherObject = {
制作:“福特”,
5“五”
“模特”:“焦点”
};
但是如果您的对象具有任何非字符串属性JavaScript将自动将它们作为字符串进行类型转换。

Instructions

创建一个代表名为myDog的狗的对象,其中包含属性"name" (字符串), "legs" "tails""friends" 。您可以将这些对象属性设置为您想要的任何值,因为"name"是一个字符串, "legs""tails"是数字, "friends"是一个数组。

Tests

tests:
  - text: <code>myDog</code>应该包含属性<code>name</code> ,它应该是一个<code>string</code> 。
    testString: 'assert((function(z){if(z.hasOwnProperty("name") && z.name !== undefined && typeof z.name === "string"){return true;}else{return false;}})(myDog), "<code>myDog</code> should contain the property <code>name</code> and it should be a <code>string</code>.");'
  - text: <code>myDog</code>应该包含属性<code>legs</code> ,它应该是一个<code>number</code> 。
    testString: 'assert((function(z){if(z.hasOwnProperty("legs") && z.legs !== undefined && typeof z.legs === "number"){return true;}else{return false;}})(myDog), "<code>myDog</code> should contain the property <code>legs</code> and it should be a <code>number</code>.");'
  - text: <code>myDog</code>应该包含属性<code>tails</code> ,它应该是一个<code>number</code> 。
    testString: 'assert((function(z){if(z.hasOwnProperty("tails") && z.tails !== undefined && typeof z.tails === "number"){return true;}else{return false;}})(myDog), "<code>myDog</code> should contain the property <code>tails</code> and it should be a <code>number</code>.");'
  - text: <code>myDog</code>应该包含属性<code>friends</code> ,它应该是一个<code>array</code> 。
    testString: 'assert((function(z){if(z.hasOwnProperty("friends") && z.friends !== undefined && Array.isArray(z.friends)){return true;}else{return false;}})(myDog), "<code>myDog</code> should contain the property <code>friends</code> and it should be an <code>array</code>.");'
  - text: <code>myDog</code>应该只包含所有给定的属性。
    testString: 'assert((function(z){return Object.keys(z).length === 4;})(myDog), "<code>myDog</code> should only contain all the given properties.");'

Challenge Seed

// Example
var ourDog = {
  "name": "Camper",
  "legs": 4,
  "tails": 1,
  "friends": ["everything!"]
};

// Only change code below this line.

var myDog = {




};

After Test

console.info('after the test');

Solution

// solution required