Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this javascript syntax mean ? (0, _parseKey2.default)(something) [duplicate]

I find this notation everywhere in Webpack generated libs but I don't understand it :

var a = (0, _parseKey2.default)(something)

What does the (0, _parseKey2.default) stands for ? I don't remember seeing those coma separated expressions between parenthesis elsewhere that in function parameters, so maybe I am just missing something simple.

Thanks for your help.

like image 419
AlexHv Avatar asked Feb 20 '16 11:02

AlexHv


People also ask

What is this this in JavaScript?

What is this? In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called). The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object.

Why use this in JavaScript?

“This” keyword refers to an object that is executing the current piece of code. It references the object that is executing the current function. If the function being referenced is a regular function, “this” references the global object.

What is this in a function?

“this” inside FunctionsThe value of this inside a function is usually defined by the function's call. So, this can have different values inside it for each execution of the function. In your index. js file, write a very simple function that simply checks if this is equal to the global object.

How does this work in Js?

In JavaScript, the this keyword allows us to: Reuse functions in different execution contexts. It means, a function once defined can be invoked for different objects using the this keyword. Identifying the object in the current execution context when we invoke a method.


1 Answers

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();
like image 89
georg Avatar answered Oct 08 '22 21:10

georg