6

I was going through the code of react-jsonschema-form.I came across following lines which I am unable to comprehend.

var formData = (0, _utils.getDefaultFormState)(schema, props.formData, definitions);

How is content within the first bracket a function to which arguments(schema, props.formData etc.) are passed?

4
  • first braces () returns a function which accepts 3 arguments.
    – Jai
    Commented Jun 21, 2017 at 6:55
  • 2
    The expression (n0, n1, ..., n) evaluates to n, so in your case, the function _utils.getDefaultFormState. As for why this is used, I'm not sure
    – Phil
    Commented Jun 21, 2017 at 6:57
  • 1
    Phil your comment would be worthy as an answer in it's own right.
    – Ryan Leach
    Commented Jun 21, 2017 at 7:00
  • 1
    What is the point of having the starting 0?
    – Ryan Leach
    Commented Jun 21, 2017 at 7:01

2 Answers 2

2

I guess the answer to this question is that in the first expression (0,_utils.getDefaultFormState) the comma , operator evaluates to the last argument and returns it.

So, comma operator operates on it's operands from left to right and returns the last right most evaluated operand in the expression.

But that is different in terms of using functions and its returned values.

// sample from MDN.
function myFunc() {
  var x = 0;

  return (x += 1, x); // the same as return ++x;
}

As i mentioned in the comment:

First brackets are self executing function and it returns it's value as a function of _utils object, which accepts 3 or more arguments.

1

In that context the first parenthesis pair is a sequence of statement whose value is the value of the last expression. Then:

(0,_utils.getDefaultFormState)

returns the function objet _utils.getDefaultFormState which is then called with the following arguments.

Not the answer you're looking for? Browse other questions tagged or ask your own question.