JavaScript syntax (0, fn)(args)
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
What is the meaning of (0, someFunction)() in javascript
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
What is meant by (0, somefunc)(args)
The (0, func)()
syntax ensures that the context (this
) in the called function func
is the global context.
For example:
var myContext = {
func: function () {
console.log(this);
}
};
myContext.func(); // => myContext
(0, myContext.func)(); // => window
What is the purpose of calling (0, func)() in JS?
The sometimes mystifying semantics of JavaScript are probably the reason. The expression
(0, _utilities.validateNextState)
evaluates of course to a reference to that function. However, because it's in that parenthesized subexpression, the function call that's outside that will be made without that _utilities
object being recognized as the context for the call (the this
value). Thus inside the validateNextState
function, this
will be either undefined
or a reference to the global object, depending on the "strict" mode state.
I suspect that Babel does that because in the original source code the call to validateNextState()
is made as if it were a "naked" function, not a method on an object. Babel doesn't know (probably) whether the value of this
matters to that particular function, but it has to make sure it gets invoked safely.
Why does google main page use (0, obj.func)(args) syntax?
This makes an indirect call.
This ensures the context, in the called function, is the global one. This might be useful in an internal scope.
Example :
var a = {
b: function(){
console.log(this);
},
c1: function(){
this.b();
},
c2: function(){
(0, this.b)();
},
c3: function(){
(this.b)();
}
}
a.c1(); // logs a
a.c2(); // logs window
a.c3(); // logs a
Why does babel rewrite imported function call to (0, fn)(...)?
(0, _b.a)()
ensures that the function _b.a
is called with this
set to the global object (or if strict mode is enabled, to undefined
). If you were to call _b.a()
directly, then _b.a
is called with this
set to _b
.
(0, _b.a)();
is equivalent to
0; // Ignore result
var tmp = _b.a;
tmp();
(the ,
is the comma operator, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator).
What does this javascript syntax mean ? (0, _parseKey2.default)(something)
This is to give _parseKey2.default
the correct this
(or, rather, no this
at all), that is, to call it as an ordinary function, not a method. Consider:
var p = {
f : function() {
console.log(this)
},
x : "foo"
};
p.f(); // { f: ... x: foo }
(p.f)(); // { f: ... x: foo }
(0, p.f)(); // implicit global this
The comma expression is a more concise way to do this:
var unbound = p.f;
unbound();
Related Topics
Localstorage Access from Local File
Reformat String Containing Date with JavaScript
HTML Alignment Issue in One MAChine Only (Both IE8)
How to Run HTML Files Directly from Github, Instead of Just Viewing Their Source
How to Develop Desktop Apps Using HTML/Css/Javascript
Uploading Image to Amazon S3 with HTML, JavaScript & Jquery with Ajax Request (No PHP)
React: Why Child Component Doesn't Update When Prop Changes
Jquery - Sticky Header That Shrinks When Scrolling Down
What Does Angular 2 Hashtags in Template Mean
Jquery VS Document.Queryselectorall
Scrolltop Animation Without Jquery
How to Resize Twitter Bootstrap Modal Dynamically Based on the Content
Create a Simple 10 Second Countdown
Closing Websocket Correctly (Html5, JavaScript)
CSS Delivery Optimization: How to Defer CSS Loading
Resize a Div on Border Drag and Drop Without Adding Extra Markup