Posted about 1 month ago. Visible to the public. Repeats.

Shorthand function properties in ES6

Here is an ES5 object literal with two string properties and a function property:

Copy
let user = { firstName: 'Max', lastName: 'Muster', fullName: function() { return this.firstName + ' ' + this.lastName } } user.fullName() // => 'Max Muster'

In ES6 we can define a function property using the following shorthand syntax:

Copy
let user = { firstName: 'Max', lastName: 'Muster', fullName() { return this.firstName + ' ' + this.lastName } } user.fullName() // => 'Max Muster'

We can also define a getter inside the object literal, without using Object.defineProperty():

Copy
let user = { firstName: 'Max', lastName: 'Muster', get fullName() { return this.firstName + ' ' + this.lastName } } user.fullName // no parentheses

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Henning Koch
Last edit:
21 days ago
by Henning Koch
Keywords:
javascript, ecmascript
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more