Possible to combine method-shorthand with fat-arro

2019-08-09 15:52发布

问题:

I know ES2015 has method shorthand:

let obj = {
  myMethod() {
    alert("STUFF!");
  }
};

I also know it has fat arrow support:

let myMethod = () => alert("STUFF!");

But is it possible to combine the two? This does not seem to work:

let obj = {
  myMethod() => alert("STUFF!")
};

The closest I could get is this, but it doesn't use the method-shorthand:

let obj = {
  myMethod: () => alert("STUFF!")
};

If not currently supported, is this something planned for future revisions?

回答1:

But is it possible to combine the two?

No.

If not currently supported, is this something planned for future revisions?

No, it is too insignificant. As you can see in your example, it can be achieved with just a single colon, and nothing else would be different.

The individual use cases make sense to me and I was surprised to find that they do not work together.

You have to consider method definitions as a fourth form of a function definition (function declaration, function expression, arrow functions, methods). As such, it doesn't make sense to "combine" them.