5

The following is from this question

function q() {
console.log((0, eval)('this'));
}

It returns [Object Window].

What I don't get is the (0,eval) part of this.

What is JS doing with this?

From the link, it says it is indirectly calling eval(). What does indirect mean?

Community
  • 1
  • 1
Greg Gum
  • 33,478
  • 39
  • 162
  • 233
  • My question was more about the (0,eval) part, which has now been pointed out as a use of the comma operator (which answers my question.) – Greg Gum Feb 06 '14 at 23:36

1 Answers1

11

Actually, just see (1,eval)('this') vs eval('this') in JavaScript?, which I've now voted as a duplicate:

.. the Ecma spec considers a reference to eval to be a "direct eval call", but an expression that merely yields eval to be an indirect one -- and indirect eval calls are guaranteed to execute in global scope.

(While the following is [mostly] true, it is not specific to eval usage.)


The comma operator evaluates all the expressions and yields the value of the last expression.

That is, (0, eval) evaluates to eval (which is a function-object value), such that the resulting expression is equivalent to eval('this').

To see it another way:

var f = (0, eval)
f === eval // true
f('this')
Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220