1

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.

qarthandso
  • 2,100
  • 2
  • 24
  • 40

1 Answers1

3

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

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143