Volver

JSBits #1: My favourite ES6 syntax

Diario del capitán, fecha estelar d64.y38/AB

Javascript ES6 Development
JSBits #1: My favourite ES6 syntax

My favourite (and most used) not-so-new JS features are: object shorthand property names and object destructuring. Despite the similitudes in their syntax, those two features are completely unrelated (but normally, they are used together).

Shorthand property names

The shorthand property name syntax allows to create an object like this:

function createPerson(name, age) {
  return { name, age };
}

That is completely equivalent to this:

function createPerson(name, age) {
  return { name: name, age: age };
}

Object destructuring

Object destructuring allows to extract variables from an object:

function printAge(person) {
  const { age } = person;
  console.log(age);
}

It's common to use it inside function parameters. This is completely equivalent to the above:

function printAge({ age }) {
  console.log(age);
}

In this way, we express clearly our intention of only using the attribute "age" of whatever comes as a parameter. We don't mind what the object is, but the shape of the object. The famous duck.

Nested property destructuring

Destructured properties can be nested, and produce expressions like this (use with caution):

const { pilot: { name, age }, color } = car;

At first can be confusing because the left side of the equal (=) looks like an object, but in fact, they are variable definitions and assignments.

The above is completely equivalent to:

const name = car.pilot.name;
const age = car.pilot.age;
const color = car.color;

Shorthand and destructuring combined

A contrived, but close to real example:

function get('/posts/:id', async (request, response) => {
  const { params: { id } } = request;

  const user = await User.findOne({ where: { id } });
  response.json(user);
})

Advanced destructuring

Is good to know that arrays can be destructured too:

const [first, second] = array

And both types can be combined:

const { users: [{ name, age }], ip } = Server

Availability

Shorthand property names are available since node v4 and most browsers. Destructuring is available since node 6 and IE > 11 browsers.

But both can be transpiled to be executed on older systems.

Summary

Shorthand property names and destructuring can make the code much clear and expressive and less verbose. But destructuring is a powerful beast. It can be hard to understand and produce complex expressions, so use it with caution.

Compartir este post

Artículos relacionados

JSBits #2: Return more than one value

JSBits #2: Return more than one value

Here's another JavaScript bits we wanted to share with you. Small tips and tricks for JavaScript developers for all levels. This time around, we share how to return more than one value.

Leer el artículo
JSBits #3: 10 things to remember

JSBits #3: 10 things to remember

Here's another JavaScript bits we wanted to share with you. 10 things to remember when coding.

Leer el artículo
JSBits #4: Comparison operator

JSBits #4: Comparison operator

Here's another JavaScript bits we wanted to share with you. Let's talk about the comparison operator, today.

Leer el artículo