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

  • SEARCH
  • Home
  • 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 7551875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:36:42+00:00 2026-05-30T10:36:42+00:00

I recently read somewhere (I am really sorry I can’t provide the source) that

  • 0

I recently read somewhere (I am really sorry I can’t provide the source) that you can use this.varname to access globals in replacement to window.varname to save 2 chars

var myVar = "global";
function myFunc() {
    var myVar = "notGlobal";
    alert( this.myVar ); //global
}

It seems to work, but I want to know if:

  • it is safe to use in old browsers
  • it is cross-browser compatible
  • it will fail under some weird circumstances
  • 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-30T10:36:43+00:00Added an answer on May 30, 2026 at 10:36 am

    I don’t think I’d do it, but it’s completely cross-browser compatible if this is referring to the global object (window). Whether it is will depend on how the function in question was called (at global scope, this does indeed refer to the global object), and whether the code in question is in “strict mode” or not. (In strict mode, this does not refer to the global object. Kudos and upvotes to Esailija for pointing that out.)

    In non-strict code:

    So at global scope:

    console.log(this === window); // true if not in strict mode
    

    And similarly, if you have a function you call directly:

    function foo() {
       console.log(this === window);
    }
    
    foo(); // Logs "true"
    

    But, in JavaScript, this is set entirely by how a function is called. So we could call foo setting this to something else:

    var obj = {};
    foo.call(obj); // Now it logs "false", `this` === `obj` during the call
    

    Similarly:

    var obj = {};
    obj.f = foo;
    obj.f(); // Also logs "false", `this` === `obj` during the call
    

    So in conclusion, at global scope (not in any function call), yes, this is reliably pointing to the global object, and if you control how the function gets called and you call it without setting this to anything else (via call or apply, or by using it from an object property a’la obj.f above), then, again, it will reliably refer to the global object. This is covered by sections 10.4.1 (Entering Global Code) and 10.4.3 (Entering Function Code) of the specification. This is how it’s been from the very beginning, I believe; certainly for the last 15 years, so you’re unlikely to find a non-compliant environment.

    More reading:

    • Mythical methods
    • You must remember this
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently read somewhere that writing a regexp to match an email address, taking
I recently read a text regarding buffer overflows that mentioned that setting breakpoints can
I recently read about a new Google-code hosted (open source) project from Google that
I recently read that the content provider content://sms shouldn't be used to access the
I recently read that you can predict the outcomes of a PRNG if you:
I recently read somewhere that one of ways of tuning SQL query is that
I recently read this thread on MSDN . So I was thinking of using
I recently read through Code Complete, and it recommends that I create a project
I recently read in a presentation on Scribd that Facebook had benchmarked a variety
Recently read in a article on dotnetpearls.com here saying that static ctors take a

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.