Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of this code (0, function) in javascript [duplicate]

Tags:

javascript

I found this code in someone's code, it sound like this:

(0, function (arg) { ... })(this) 

After I try to play around like below,

(0, function (arg) { console.log(arg) })(2);  console.log((0, 1, 2, 3));  (0, function plus1 (arg) { console.log(arg + 1) }, function plus2 (arg) { console.log(arg + 2) })(5);

I found that it will always return last item in the bracket.

I wonder what is the name of this programming pattern and what is the use case?

like image 675
Simon Avatar asked Dec 05 '16 04:12

Simon


People also ask

What does 0 mean in JavaScript?

0 is an argument passed to void that does nothing, and returns nothing. JavaScript code (as seen above) can also be passed as arguments to the void method. This makes the link element run some code but it maintains the same page.

Can we return 0 in JavaScript?

Returning 0 from main tells the operating system that the program executed successfully. Every value has a different meaning but all in all, a non-zero value returned from main() means unsuccessful execution of the program.

Why is JavaScript running twice?

It's because a <label> with a for attribute raises the click event of <input type="checkbox"> element that is associated for when clicked. You should bind click event handler to input , not to label . Show activity on this post. When you click your label, may it be there is event click runs also on checkbox?

What does this => mean in JavaScript?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.


2 Answers

In this particular case it seems superfluous, but sometimes this approach is useful.

For example, with eval:

(function() {    (0,eval)("var foo = 123"); // indirect call to eval, creates global variable  })();  console.log(foo);            // 123  (function() {    eval("var bar = 123");     // direct call to eval, creates local variable  })();  console.log(bar);            // ReferenceError

It's also useful when you want to call a method without passing the object as the this value:

var obj = {    method: function() { return this; }  };  console.log(obj.method() === obj);     // true  console.log((0,obj.method)() === obj); // false

Also note that, depending on the context, it might be the arguments separator instead of a comma operator:

console.log(    function(a, b) {      return function() { return a; };    }    (0, function (arg) { /* ... */ })(this)  ); // 0
like image 139
Oriol Avatar answered Oct 12 '22 22:10

Oriol


typical example could be,

for(var i=0,j=10; i < j; i++){  // code ... } 

comma operator would evaluate expressions from left-to-right and return result of right most expression

// e.g.    var a = 1, b= 2, c = 3, d = function(){ console.log("a => " +  a) }()
like image 30
A.T. Avatar answered Oct 13 '22 00:10

A.T.