0

obj.B in this example:

var obj = {
    A: function(arg) {
        //...
    }
    B(arg) {
        //...
    }
}

In firefox (79), obj.A and obj.B are identical. In IE 11, obj.B is a syntax error. I want more compatibility info but have failed to find anything about this syntax because most search engines ignore "punctuation". I keep hitting tutorials about functions in general or arrow operators.

This does appear somewhat similar to the arrow operator, with the notable difference that, unlike a named arrow function, this syntax does create a scope, pointing this to obj when called like obj.B()

Could anyone tell me what versions of other browsers this works in, or at least a searchable name for this function abbreviation?

memtha
  • 797
  • 5
  • 24
  • 1
    `B` was new in ES2015 (hence why it doesn't work in IE11) and is termed "shorthand method name"; `A` is just a property that happens to be a function (i.e., a method). See [Object initializer on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer). – Heretic Monkey Aug 25 '20 at 21:01
  • Yup, that's what I was looking for. Thanks. – memtha Aug 25 '20 at 21:59
  • It's called a "method definition" (just like in a `class`) and was introduced with ES6. Btw it's not identical to the `function` expression, as `B` doesn't have a `.prototype` and cannot be used as a constructor (indeed similar to an arrow function, but without lexical `this`). – Bergi Aug 25 '20 at 22:03

0 Answers0