4

I read a line from doT.js:

var global = (function(){ return this || (0||eval)('this'); }());

After it was minified:

l=function(){return this||(0,eval)("this")}();

So what is the (0,eval), I mean what does the comma do?

I played in Chrome's console, (0,1), (2,1), (2,{}), 2,1, etc, it always returns the last one.

JstnPwll
  • 8,585
  • 2
  • 33
  • 56
Mengdi Gao
  • 780
  • 8
  • 23
  • 2
    Do we know why they used `(0||eval)` in the first place? – Asherah May 12 '12 at 14:13
  • It very well might be to get code analyzers to shut up about the fact that they're using the evil `eval` at all. But I'm just as confused as you are. – btown May 12 '12 at 14:26
  • 2
    @Len They want an "indirect call to `eval`": https://github.com/olado/doT/issues/26#issuecomment-5669788 – Mengdi Gao May 13 '12 at 01:10

1 Answers1

4

The comma operator evaluates both and always returns the last. Much like you said.

You can read up on the comma operator: http://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/

Even though I have no idea the purpose of (0||eval)... (0,eval) is the equivalent and one less character.

natedavisolds
  • 4,305
  • 1
  • 20
  • 25
  • In the "Indirect calls to eval" section of the linked page, it explains that by using `(0,eval)`, "we can use the comma operator to fashion an indirect call to eval which will force it to execute in the global context". – cokeman19 Oct 09 '20 at 20:33