Stumbled upon an interesting shortcut in JS. To create an object with functions, I used the following code fragment:

var a = { func1: function(){ }, func2: function(){ } }; 

Somehow I accidentally wrote this:

 var a = { func1(){ }, func2(){ } }; 

That the first, that the second option in function - the same (IMHO). Question: How long has such a reduction been working (there are suspicions that came from ECMA 6)? Is it worth it to cut? Are there any disadvantages of this method?

  • one
    Unreadable, most likely, not easier then minify? - Jean-Claude

2 answers 2

This feature is added to ECMA2015 .

In addition to getters and setters, you can now declare methods and generators in the object's initializer.

MethodDefinition [Yield] :
PropertyName [? Yield] (StrictFormalParameters) {FunctionBody}
GeneratorMethod [? Yield]
get PropertyName [? Yield] () {FunctionBody}
set PropertyName [? Yield] (PropertySetParameterList) {FunctionBody}

There is no difference with the previous record, so you can use any form, and even all of them together.

Use the real classes that appeared in the new standard.

I also advise you to try TypeScript, which may be the same as Javascript, and even more. At the same time, it can be converted to regular JavaScript.