Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

(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;
}());
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...