Can methods be defined in an object initializer without using the `function` keyword?

后端 未结 1 1495
孤独总比滥情好
孤独总比滥情好 2020-12-04 02:31

The other day I was a bit tired, I wrote this JavaScript code:

var obj = {a(toto){console.log(\"func a: \", toto);} };         


        
相关标签:
1条回答
  • 2020-12-04 03:05

    This is ECMAScript 6 syntax. Depending on your environment - node vs browser - it may or may not be advisable to use this syntax (e.g. not supported cross browser).

    Given the following code:

    var obj = { foo: function() {}, bar: function() {} };

    You are now able to shorten this to:

    var obj = { foo() {}, bar() {} };

    Reference: Method definitions (ES6)

    0 讨论(0)
提交回复
热议问题