1

According to the Mozilla Developer Network, the definition of an object in JS is:

"An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. "

To me this means an object's method should be a key, value pair. Like this:

var myObject = {
  info: 'Im an object',
  sayInfo: function() {
    console.log(this.info);
  }
};

Above, the method is the key called "sayInfo" and the value is a function which logs to the console.

But I've seen a method on an object defined this way:

var myObject = {
  info: 'Im an object',
  sayInfo() {
    console.log(this.info);
  }
};

I don't understand how the method sayInfo is syntactically possible because it is not a key, value pair. How is it possible to define a method this way?

Mjuice
  • 1,288
  • 2
  • 13
  • 32
  • 3
    This is likely just a shortcut, where the name of the function is the key, and the value is an anonymous function. – Carcigenicate Feb 09 '17 at 05:35
  • As @Carcigenicate said, this is indeed a shortcut introduced in ES6. Check [here](http://es6-features.org/#MethodProperties). – Soubhik Mondal Feb 09 '17 at 05:39
  • Where on MDN did you read this? [The page about object literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Method_definitions) lists method definitions too. – Felix Kling Feb 09 '17 at 05:46

0 Answers0