1.6 KiB
1.6 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7b8a367417b2b2512b4f | 使用簡單字段編寫簡潔的對象字面量聲明 | 1 | 301225 | write-concise-object-literal-declarations-using-object-property-shorthand |
--description--
ES6 添加了一些很棒的功能,用於更方便地定義對象。
請看以下代碼:
const getMousePosition = (x, y) => ({
x: x,
y: y
});
getMousePosition
簡單的函數,返回擁有兩個屬性的對象。 ES6 提供了一個語法糖,消除了類似 x: x
這種冗餘的寫法。 你可以只寫一次 x
,解釋器會自動將其轉換成 x: x
(或效果相同的內容)。 下面是使用這種語法重寫的同樣的函數:
const getMousePosition = (x, y) => ({ x, y });
--instructions--
請使用簡單屬性對象的語法來創建並返回一個具有 name
、age
和 gender
屬性的對象。
--hints--
createPerson("Zodiac Hasbro", 56, "male")
應該返回 {name: "Zodiac Hasbro", age: 56, gender: "male"}
。
assert.deepEqual(
{ name: 'Zodiac Hasbro', age: 56, gender: 'male' },
createPerson('Zodiac Hasbro', 56, 'male')
);
不要使用 key:value
。
(getUserInput) => assert(!getUserInput('index').match(/:/g));
--seed--
--seed-contents--
const createPerson = (name, age, gender) => {
// Only change code below this line
return {
name: name,
age: age,
gender: gender
};
// Only change code above this line
};
--solutions--
const createPerson = (name, age, gender) => {
return {
name,
age,
gender
};
};