How does this definition of an object object work without the keyword "function"?

I discovered this by accidentally leaving the function keyword. Usually the foobar method in the module below will be declared as foobar: function(arg1) , but it is interesting that this works, at least in some browsers, for example. Chrome version 44.0.2403.157 m, but it does not work in IE 11.0.9600.17959

How is it possible that this generally works in any browser? Is this some kind of new ES6 functionality?

 var module = { foobar(arg1) { alert(arg1); } }; module.foobar("Hello World"); 
+13
javascript function methods ecmascript-6
Sep 04 '15 at 18:39
source share
2 answers

How is it possible that this generally works in any browser? Is there any new ES6 functionality?

Yes

...

Method Definitions

An object property can also refer to a function or getter or setter method.

 var o = { property: function ([parameters]) {}, get property() {}, set property(value) {}, }; 

ECMAScript 6 has an abbreviated notation, so the keyword β€œfunction” is no longer required.

 // Shorthand method names (ES6) var o = { property([parameters]) {}, get property() {}, set property(value) {}, * generator() {} }; 

...

+15
Sep 04 '15 at 18:42
source share

ES6 allows for β€œconcise methods,” which you discovered are not yet cross-browser compatible.

+4
04 Sep '15 at 18:42
source share



All Articles