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 9115667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:29:11+00:00 2026-06-17T04:29:11+00:00

Introduction I use Google Closure Compiler frequently for compressing my JavaScript files. Now, it

  • 0

Introduction

I use Google Closure Compiler frequently for compressing my JavaScript files. Now, it seems to compress my code fairly well.

Now, I try to make a habit of storing the this object in a local variable, because this cannot be obfuscated, but a local variable certainly can be obfuscated. However, Google Closure Compiler does not recognize this, and instead removes all instances of the local variable, replacing it with this.

Regarding optimization…

I am well aware that one should avoid pre-optimization when writing code. However, I feel that caching this is acceptable because doing so provides clarity (because this can have many contexts, and referencing it by another name will reduce ambiguity).

Example

The code below is pretty basic, and I understand that it may be poorly written. However, the code will demonstrate exactly the issue which I am facing.

Here is the original source file, before compression:

(function() {
  var that = this;
  that.a = 3;
  that.b = 4;
  this.c = 5;
  return that;
}());

Now here is the compressed source file. Note that the assignment of this to that has been removed.

(function(){this.a=3;this.b=4;this.c=5;return this})();

Ideally, I would expect the assignment to that to remain in some form, perhaps something similar to this:

(function(){var t=this;t.a=3;t.b=4;t.c=5;return t})();

Now, the code above hardly saves any bytes, but when working with a much larger script (as I often do), the savings most definitely add up.

Question

In short, how can I prevent the Closure Compiler from removing the that variable in my above script?

  • 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-06-17T04:29:12+00:00Added an answer on June 17, 2026 at 4:29 am

    You are attempting to out-think the compiler. It’s a losing battle. However, here’s the two main reasons people try to do this type of thing.

    1. Reduce size of code. The theory being that a single letter variable is smaller than the keyword this. However, this theory is flawed in most cases. See the compiler FAQ.

    2. Prevent the context of the keyword this from changing. However, in SIMPLE_OPTIMIZATIONS this is unnecessary. If you create an inner closure that references your variable, the compiler will not inline the value. Under ADVANCED_OPTIMIZATIONS, using the keyword this can be dangerous outside of a prototype function or constructor and should be done with care. See an article explaining why.

    If you really want to prevent the compiler from inlining your value, you’ll need to add it as a property on an object using quoted syntax:

    (function() {
      var config = {};
      config['that'] = this;
    })()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://code.google.com/apis/libraries/devguide.html I don't want to use Google's API key to load the various JS
every introduction and sample that I can find seems to use GLUT or some
I tried to use this code but it doesn't work. http://developer.apple.com/library/ios/#samplecode/PageControl/Introduction/Intro.html Ld /Users/waitonza/Library/Developer/Xcode/DerivedData/Dr_Ngoo-aanknxmuodcgjicaigxevljxokeq/Build/Products/Debug-iphonesimulator/Dr Ngoo.app/Dr
Introduction: Now I know this question could be very broad and it would be
Introduction: I want to loop through XML files with flexible categories structure. Problem: I
Introduction I am trying to use P/Invoke to register a struct of callbacks with
Introduction Hello, I'm that typical programmer that know how to use api, but tend
I'm starting to use Dojo; this is (essentially) my introduction to AJAX. We have
Every now and then I use the mdb debugger to examine core dumps on
I want to use part of a blog post as the introduction to the

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.