liveBook · Manning

Shorthand method name

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A JavaScript object-literal method syntax that defines a function without the `function` keyword and binds it as an object property.

When an object literal uses a function as a property value, shorthand method syntax removes the redundant colon and `function` keyword: write `update(changes) { ... }` instead of `update: function(changes) { ... }`. The parentheses signal a method. section 12.0 section 12.2

function createStateManager() {
  const state = {};
  return {
    update(changes) {
      Object.assign(state, changes);
    },
    getState() {
      return Object.assign({}, state);
    }
  }
}

A state manager shows concise methods used for updating and reading private state. section 12.2

A shorthand method evaluates to an anonymous function. It cannot refer to itself by its method name, so avoid this syntax for recursion. Calling itself through `this.at` can work only while `this.at` still points to the function; detaching or renaming the method breaks that assumption. section 12.2

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Explains the concise form and anonymous-function behavior, then warns against self-reference and details the fragile `this.at` workaround.
Examples
Uses a state manager and Fibonacci recursion to contrast concise methods with function-valued properties.
What you can do after
Apply shorthand method syntax to define object-literal functions without redundant `function` and colon syntax.
How it connects
Connects object literals with anonymous functions, recursion, and `this`-based method calls.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.