Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this expression do? Why is it useful? [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 886
AlexHv Avatar asked Nov 20 '22 08:11

AlexHv


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 158
georg Avatar answered Jan 14 '23 16:01

georg