Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:50:10+00:00 2026-05-28T22:50:10+00:00

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

  • 0

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;
}());
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:50:11+00:00Added an answer on May 28, 2026 at 10:50 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have some code to read from the start to end of a
I just read about HTML 5's WebSocket interface. How can I start trying this?
Before you start marking this as a duplicate , read me out. The other
I know it is really annoying to read this topic again. Before you start
I've read quite a few articles and questions on calculating date differences in javascript,
I just started to read some JavaScript project. Most of the .js file to
I read this a lot in many JavaScript introductions. I just don't understand it.
I read a lot of posts that convinced me I should start writing unit
I'm looking to sort the start menu alphabetically using C#. I've read about deleting
I start by creating a string variable with some non-ascii utf-8 encoded data on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.