3

i was trying to learn and understand KnexJS source Code, until i came to a block of code which i don't have any idea what is it.

  config = (0, _lodash.assign)({}, config, {
  connection: (0, _parseConnection2.default)(config.connection).connection
});

in Simplified version

config = (0, Args1)({}, config, {Args2});

what makes me confused is that,there is no 'function' keywords or any 'identifier' before the parentheses...

i have found same but not similiar question here: double parentheses and that is understandable and different...is it Valid JS? if Yes, what it's mean?

1 Answers1

2

It's the comma operator (evaluates a comma-separated list of expressions, resolving to the value of the last expression), combined with a call of _lodash.assign with a global calling context, rather than a calling context of _lodash. Here's a simpler example:

const obj = {
  abc: function(arg) {
    console.log(this);
    console.log('abc called with ' + arg);
  }
}
obj.abc('foo');
(0, obj.abc)('foo')

As you can see, calling obj.abc alone results in the calling context being obj, while (0, obj.abc)('foo') results in the calling context being window. I think it's a minification technique - saner, more understandable code would look like

obj.abc.call(window, 'foo')

or

const theFn = obj.abc;
theFn('foo')
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320