Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(1, eval)('this') vs eval('this') in JavaScript?

I start to read JavaScript Patterns, some codes confused me.

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

Here are my questions:

Q1:

(1, eval) === eval?

Why and how does it work?

Q2: Why not just

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

or

 var global = (function () {     return this; }()); 
like image 461
shawjia Avatar asked Feb 02 '12 04:02

shawjia


People also ask

What does eval () do in JavaScript?

The eval() function evaluates JavaScript code represented as a string and returns its completion value. The source is parsed as a script.

Which is faster eval () or new function?

`Function() is a faster and more secure alternative to eval().

Why we should not use eval in JavaScript?

Malicious code : invoking eval can crash a computer. For example: if you use eval server-side and a mischievous user decides to use an infinite loop as their username. Terribly slow : the JavaScript language is designed to use the full gamut of JavaScript types (numbers, functions, objects, etc)… Not just strings!

What is the alternative of eval in JavaScript?

eval(code) can artificially be replaced by ( new Function('return '+code))() but then it's better to keep with eval and explicitly say in comments why you do it, and in your case, your variables would be in the wrong scope.


2 Answers

The difference between (1,eval) and plain old eval is that the former is a value and the latter is an lvalue. It would be more obvious if it were some other identifier:

var x; x = 1; (1, x) = 1; //  syntax error, of course! 

That is (1,eval) is an expression that yields eval (just as say, (true && eval) or (0 ? 0 : eval) would), but it's not a reference to eval.

Why do you care?

Well, 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.

Things I still don't know:

  1. Under what circumstance does a direct eval call not execute in global scope?
  2. Under what circumstance can the this of a function at global scope not yield the global object?

Some more information can be gleaned here.

EDIT

Apparently, the answer to my first question is, "almost always". A direct eval executes from the current scope. Consider the following code:

var x = 'outer'; (function() {   var x = 'inner';   eval('console.log("direct call: " + x)');    (1,eval)('console.log("indirect call: " + x)');  })(); 

Not surprisingly (heh-heh), this prints out:

direct call: inner indirect call: outer 

EDIT

After more experimentation, I'm going to provisionally say that this cannot be set to null or undefined. It can be set to other falsy values (0, '', NaN, false), but only very deliberately.

I'm going to say your source is suffering from a mild and reversible cranio-rectal inversion and might want to consider spending a week programming in Haskell.

like image 145
Michael Lorton Avatar answered Oct 26 '22 15:10

Michael Lorton


The fragment

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

will correctly evaluate to the global object even in strict mode. In non-strict mode the value of this is the global object but in strict mode it is undefined. The expression (1, eval)('this') will always be the global object.

The reason for this involves the rules around indirect versus direct eval. Direct calls to eval has the scope of the caller and the string this would evaluate to the value of this in the closure. Indirect evals evaluate in global scope as if they were executed inside a function in the global scope.

Since that function is not itself a strict-mode function the global object is passed in as this and then the expression 'this' evaluates to the global object. The expression (1, eval) is just a fancy way to force the eval to be indirect and return the global object.

A1: (1, eval)('this') is not the same as eval('this') because of the special rules around indirect versus direct calls to eval.

A2: The original works in strict mode, the modified versions do not.

like image 20
chuckj Avatar answered Oct 26 '22 13:10

chuckj