ES6: Write Concise Object Literal Declarations Using Object Property Shorthand
ES6 adds some nice support for easily defining object literals.
Consider the following code:
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:
const getMousePosition = (x, y) => ({ x, y });
Use object property shorthand with object literals to create and return an object with name
, age
and gender
properties.
Here is my solution which also worked :
const createPerson = (name, age, gender) =>({name, age, gender});
var jeff = createPerson(“Jeff”, 21, “male”);
console.log(jeff.gender);
//outputs “male”
Another syntax :
const createPerson = (name, age, gender) => {
“use strict”;
// Only change code below this line
return ({name, age, gender});
// Only change code above this line
};
var jeff = createPerson(“Jeff”, 21, “male”);
console.log(jeff.age);
//outputs 21