3

I found there are some expressions in some libs like this:

exports.default = (0, _createHelper2.default)(pure, 'pure', 1)

Seems like it has no difference with _createHelper2.default(pure, 'pure', 1)

So what is the purpose of writing like this?

kaze13
  • 309
  • 2
  • 12

1 Answers1

4

There's a small difference: The value of this used when calling _createHelper2.default:

With

exports.default = (0, _createHelper2.default)(pure, 'pure', 1)

_createHelper2.default will be called with this set to either the global object (loose mode) or undefined (strict mode).

With

_createHelper2.default(pure, 'pure', 1)

_createHelper2.default will be called with this set to _createHelper2.

(Whether _createHelper2.default actually sees the this value used for the call depends on uses on whether it's a normal function, a bound function, or an arrow function; but that's the difference in the call to it.)

exports.default = (0, _createHelper2.default)(pure, 'pure', 1) works by using the comma operator to get the function reference without associated property information, and then calls that function not via a property access, which bypasses the usual setting of this. So it's like doing this:

var f = _createHelper2.default;
exports.default = f(pure, 'pure', 1)
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • why then not calling it with an obvious `call` which is way easier to understand later? `_createHelper2.default.call(undefined, pure, 'pure', 1)` – smnbbrv Apr 28 '16 at 10:55
  • @smnbbrv: It may well be minified code, not what a human actually wrote or needs to edit. And some people like a really terse style. (I'm not one of them.) Still, like idioms in many languages, it's perfectly clear once you're familiar with it. – T.J. Crowder Apr 28 '16 at 11:02
  • yes, sure, just looks awkward... I have no problem with similar syntax but I can imagine how the newcomers could hate for it :) – smnbbrv Apr 28 '16 at 11:07