qarthandso
qarthandso

Reputation: 2188

ES6 Object Literal Syntax for Methods

I'm looking at this this page about various shorthand syntaxes in ES6 for declaring methods inside of objects.

I'm not understanding the differences between these two forms:

var foo = {
    a() {},
    b() {}
};

and

var foo = {
    x: (y) => y
};

The article seems to make a clear distinction between these two formats, but doesn't the first one really just become the second? If we wanted to include parameters, we'd just do a(y) {} in the first one.

Upvotes: 1

Views: 334

Answers (1)

Felix Kling
Felix Kling

Reputation: 816462

but doesn't the first one really just become the second?

No. The method syntax is more equivalent to using a function expression:

var foo = {
  a: function() {},
};

If you'd assign an arrow function then you won't be able to access the object via this.

And of course an empty function (function() {}) is not the same as the identity function (function(x) { return x; }).


See also

Upvotes: 3

Related Questions