jskatas.org Continuously Learn JavaScript. Your Way.

Object literal: basics

ES6 has new shorthands for objects.

The object literal allows for new shorthands

with variables

the short version for {x: x} is {x}
const y = 2; const short = {x}; assert.deepEqual(short, {y: y});
works with multiple variables too
const x = 1; const y = 2; const short = {x, y: z}; assert.deepEqual(short, {x: x, y: y});

with methods

using the name only uses it as key
const func = () => func; const short = {it}; assert.deepEqual(short, {func: func});
a different key must be given explicitly, just like before ES6
const func = () => func; const short = {func}; assert.deepEqual(short, {otherKey: func});
inline functions, can written as obj={func(){}} instead of obj={func:function(){}}
const short = { inlineFunc: 'I am inline' }; assert.deepEqual(short.inlineFunc(), 'I am inline');

Required Knowledge

Related Katas

Global Object API

Object API

Object literal

Difficulty Level

INTERMEDIATE

First Published

25 March 2015

Stats

5 tests to solve