Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to understand javascript syntax [duplicate]

Tags:

javascript

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?

like image 994
john doe Avatar asked Jun 21 '17 06:06

john doe


Video Answer


2 Answers

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.

like image 192
Jai Avatar answered Sep 30 '22 10:09

Jai


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.

like image 43
Jean-Baptiste Yunès Avatar answered Sep 30 '22 10:09

Jean-Baptiste Yunès