Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript syntax (0, fn)(args)

Tags:

javascript

Just checking Google's JavaScript code and I've found this syntax:

var myVar = function...; (0, myVar)(args); 

Do you know the meaning of this syntax? I cannot find the difference between (0, myVar)(args); and myVar(args);.

To give an exact example, we have

_.x3 = function (a, b) {     return new _.q3(20 * b.x + a.B.B.x, 20 * b.y + a.B.B.y) }; 

And later

this.ta = new _.s3((0, _.x3)(this.fa, this.B.B), 0); 
like image 206
korko Avatar asked Jul 18 '12 12:07

korko


People also ask

What is args in JS?

args is a rest parameter. It always has to be the last entry in the parameter list and it will be assigned an array that contains all arguments that haven't been assigned to previous parameters. It's basically the replacement for the arguments object. Instead of writing function max() { var values = Array. prototype.

What is function () () in JavaScript?

A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious relationship between the input and the output.

What are the 3 types of functions in JavaScript?

There are 3 ways of writing a function in JavaScript: Function Declaration. Function Expression. Arrow Function.

What is the value of 0 in JavaScript?

In JavaScript “0” is equal to false because “0” is of type string but when it tested for equality the automatic type conversion of JavaScript comes into effect and converts the “0” to its numeric value which is 0 and as we know 0 represents false value. So, “0” equals to false.


2 Answers

I had the same question and then found the answer, as follows:

It really is for

(0, foo.fn)(); 

Remember that in JavaScript, when foo.fn() is invoked, then inside of fn, the this is bound to foo. If you use

var g = foo.fn; g(); 

then when g is invoked above, the this is bound to the global object (window, in the context of a web browser).

So do you need to define g like above? Can you do something such as

(foo.fn)(); 

The answer is no. JavaScript will treat it the same as foo.fn(); as it is just foo.fn with the redundant () that can be removed.

But there is one way to get around it, and it is exactly to use the comma operator, which Mozilla stated as

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand

So using

(0, foo.fn)(); 

the (0, foo.fn) will get evaluated to a reference to the function, like g above, and then the function is invoked. And then, this is not bound to foo but is bound to the global object.

So the code written this way, is to "cut the binding".

Example:

var foo = {                fullName: "Peter",                sayName:  function() { console.log("My name is", this.fullName); }            };  window.fullName = "Shiny";  foo.sayName();       // My name is Peter  (foo.sayName)();     // My name is Peter  (0, foo.sayName)();  // My name is Shiny

Now why would some code want to cut the binding? I read that in some case, if we have a function:

function foo() {   // using `this` here } 

Then the this would point to the global object. But if foo() together with other functions and values, are packaged into a module, then when the function is invoked using

someModule.foo(); 

Then the this is bound to someModule, and it has changed the behavior of foo(). So to keep foo() as original as it is, we cut the binding, so that inside of foo(), the this is bound to the global object like before.

like image 180
nonopolarity Avatar answered Sep 22 '22 20:09

nonopolarity


This syntax is using the comma operator, ,. It evaluate all of its operands and returns the value of the last one. In this case, 0 is just being used as a placeholder so (0, function() {}) will return (function() {}). After it is evaluated, the (args) part is invoking the function and giving it its arguments.

Edit after comment:

The reason one would use this style of coding is so that they can execute the code quickly or on one line. Here is an example:

var a = 0,     b = 1,     c;  c = ( a++, b++, a + 2 ); // a is added, b is added, and a is added then returned  a; // 1 b; // 2 c; // 3 
like image 40
David G Avatar answered Sep 22 '22 20:09

David G