76 lines
1.7 KiB
Markdown
76 lines
1.7 KiB
Markdown
![]() |
---
|
||
|
id: 587d7b8a367417b2b2512b4f
|
||
|
title: Write Concise Object Literal Declarations Using Object Property Shorthand
|
||
|
challengeType: 1
|
||
|
forumTopicId: 301225
|
||
|
dashedName: write-concise-object-literal-declarations-using-object-property-shorthand
|
||
|
---
|
||
|
|
||
|
# --description--
|
||
|
|
||
|
ES6 adds some nice support for easily defining object literals.
|
||
|
|
||
|
Consider the following code:
|
||
|
|
||
|
```js
|
||
|
const getMousePosition = (x, y) => ({
|
||
|
x: x,
|
||
|
y: y
|
||
|
});
|
||
|
```
|
||
|
|
||
|
`getMousePosition` is a simple function that returns an object containing two properties. ES6 provides the syntactic sugar to eliminate the redundancy of having to write `x: x`. You can simply write `x` once, and it will be converted to`x: x` (or something equivalent) under the hood. Here is the same function from above rewritten to use this new syntax:
|
||
|
|
||
|
```js
|
||
|
const getMousePosition = (x, y) => ({ x, y });
|
||
|
```
|
||
|
|
||
|
# --instructions--
|
||
|
|
||
|
Use object property shorthand with object literals to create and return an object with `name`, `age` and `gender` properties.
|
||
|
|
||
|
# --hints--
|
||
|
|
||
|
`createPerson("Zodiac Hasbro", 56, "male")` should return `{name: "Zodiac Hasbro", age: 56, gender: "male"}`.
|
||
|
|
||
|
```js
|
||
|
assert.deepEqual(
|
||
|
{ name: 'Zodiac Hasbro', age: 56, gender: 'male' },
|
||
|
createPerson('Zodiac Hasbro', 56, 'male')
|
||
|
);
|
||
|
```
|
||
|
|
||
|
Your code should not use `key:value`.
|
||
|
|
||
|
```js
|
||
|
(getUserInput) => assert(!getUserInput('index').match(/:/g));
|
||
|
```
|
||
|
|
||
|
# --seed--
|
||
|
|
||
|
## --seed-contents--
|
||
|
|
||
|
```js
|
||
|
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--
|
||
|
|
||
|
```js
|
||
|
const createPerson = (name, age, gender) => {
|
||
|
return {
|
||
|
name,
|
||
|
age,
|
||
|
gender
|
||
|
};
|
||
|
};
|
||
|
```
|