-1

Problem I am having is that when Yarn/NPM generate the code I see function binding changes. I have not seen anywhere on the style of binding

I have tried to check but could not find any documentation on it.

Here is a function defined in one of the files in the project directory named model.js

 export function buildModel(modelType, numTimeSteps, numFeatures) {..}

It is very clear this function takes in 3 parameters. Now when I do a Yarn Build, this combines/rearranges all the js files & now when I look at the function it shows up like this

 const model = (0, _models.buildModel)(modelType, steps, numFeatures); 

I do not understand this syntax style

Need details or resource link to understand this generated code style.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
Integration
  • 337
  • 1
  • 4
  • 15
  • Those are not the same. The later will probably call the transpiled function shown in the first snippet. So therefore there is not much to understand, those are entirely different. – Jonas Wilms Jul 22 '19 at 20:44
  • @JonasWilms I'm not sure I follow; it looks like some generated code to get at a property of an object (the leading parenthesized comma expression). – Pointy Jul 22 '19 at 20:46
  • @pointy "now when I look at the function it shows up like this" ... thats not the function ... – Jonas Wilms Jul 22 '19 at 20:48
  • @JonasWilms ah I see, right; it's an *invocation* of the function (I think) – Pointy Jul 22 '19 at 20:48

1 Answers1

1

The expression (0, _models.buildModel) evaluates to _models.buildModel; it's a parenthesized comma expression. Thus the overall effect is

(_models.buildModel)(modelType, steps, numFeatures);

The _models symbol is I presume the object created during the build for symbols exported from that module. The code you're looking at is an invocation (a call) of the function, not its definition.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Any idea what the `0` is for ? – Håken Lid Jul 22 '19 at 20:51
  • 2
    @HåkenLid See the duplicate which explains it properly – Bergi Jul 22 '19 at 20:52
  • 1
    @HåkenLid right the linked dup explains it; it's to make sure the function is called "naked" with `this` bound appropriately; it's an arcane JavaScript function call detail. – Pointy Jul 22 '19 at 20:54
  • Ok, so if I understand correctly, the 0 is just there because the comma operator needs two operands and `0` is the shortest possible expression to put on the left hand. – Håken Lid Jul 22 '19 at 21:02
  • Yes that's correct. – Pointy Jul 22 '19 at 21:03
  • Thanks for the explanation, it cleared my thinking, how come one does not see any literature on this! – Integration Jul 22 '19 at 21:21
  • @Integration well code transpilers have to do all sorts of things to ensure that code runs the way the source language implies that it should. – Pointy Jul 22 '19 at 21:29