Posted 4 months 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
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Henning Koch
Last edit:
3 months 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