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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:33:07+00:00 2026-06-13T13:33:07+00:00

I have production code that I am now trying to minify with the Closure

  • 0

I have production code that I am now trying to minify with the Closure Compiler using the advanced option. The code uses Paul’s client side cookie libraries. The compiler generates over 30 of these warnings:

JSC_USED_GLOBAL_THIS: dangerous use of the global this object

Here are some code snippets from Paul’s library (oroginal code uses named functions, not anonymous functions):

  var cookieObject = function (name, expires, accessPath) {
        var i, j
        this.name = name
        this.fieldSeparator = "#"
        // this.found = false
        this['found'] = false;  // don't allow Closure-Compiler to rename
        this.expires = expires
        this.accessPath = accessPath
        this.rawValue = ""
        this.fields = new Array()
        this.fieldnames = new Array() 
        if (arguments.length > 3) { // field name(s) specified
              j = 0
              for (i = 3; i < arguments.length; i++) {
                    this.fieldnames[j] = arguments[i]    
                    j++
              }
              this.fields.length = this.fieldnames.length 
        }
        this.read = ucRead            // function assignments
        this.write = ucWrite
        this.remove = ucDelete
        this.get = ucFieldGet
        this.put = ucFieldPut
        this.namepos = ucNamePos
        this.read()
  }; // cookieObject

The uc functions are typically constructed this way:

  var ucRead = function () {
        var search = this.name + "="
        var CookieString = document.cookie
        this.rawValue = null
        this.found = false
  }

I have read How does “this” keyword work within a JavaScript object literal? as well as Closure Compiler Warning dangerous use of the global “this” object? and Scope in JavaScript. However, I still don’t understand what the scope of this is in the above code nor how I would go about rewriting this code to eliminate the compiler warnings.

Q1: Can someone clarify for me what the scope of this is?

Q2: Can someone provide some code snippets showing how the above can be rewritten to eliminate the compiler warnings (I assume the use of this has to be eliminated)?

TIA.

  • 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-13T13:33:08+00:00Added an answer on June 13, 2026 at 1:33 pm

    The scope of this is, essentially, unpredictable. this references whatever context in which the function is being called that particular call. For example:

    var foo = function()//or function foo()
    {
        console.log(this);
    };
    foo();//logs the window object
    var anObj = {name:'someObject',method:foo};
    anObj.method();//logs `anObj`
    

    The basics are simple:

    [[someObject]].function();//call func
        /\            ||
        ||  implicit  ||
        |______________|
    

    In the irst example there was no explicit owner object, so JS falls back to the global object, by default. (unless when using strict mode). The second example, foo was used as a method, called from the object, so this references that object.

    Now, this is all pretty easy but given that the this reference is determined ad hoc, and functions are loosely bound in JS (Array.prototype.slice.apply(arguments,[0]);), There is no guarantee that this will always point to what you expect it to point to.
    ECMAScript 5 offers you the bind method for that, enabling you to bind a given context to a function object, but don’t forget: there are some annoying people out there who still use outdated browsers.
    Besides, this is an “issue” which has been around for a long time, and people have used various workarounds. The easiest is the use of closures:

    var MyConstructor = function()
    {
        "use strict";//in strict mode, this'll throw errors when called without new keyword
        if (this === window || !this)
        {
            throw 'Omitted new keyword in older browsers, that don\'t support strict mode';
        }
        var that = this;//create closure reference to the new instance
        that.someProperty = 'Use that instead of this';
        that.especially = function()
        {
            console.log(this === that);//see later
            console.log('especially in methods, because they can be borrowed, at which point `this` could reference anything, but here: ' + that + ' Will always be what you expect');
        };
    }
    var foo = new MyConstructor();
    foo.especially();//logs true, and 'especially in methods ...'
    bar = [];
    foo.especially.apply(bar,[]);//logs false, but the second log will still be the same
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code that sets a new repeating alarm (on production I'll use
I have a particular file in a git source code repository that contains production
We are trying to debug some legacy code. We have found that we are
I have a bit of code that fits theoretical prediction to experimental data, and
I have a solution with two projects. One for project for the production code
While running code in production, I don't have to interact with the Ember.js runloop
We have two code bases for testing and production. The only differences between these
We have a setup where most code, before being promoted to full production, is
I have generated a pdf file with password protection by using the following code:
I am trying to prove a table design flaw in a production db, that

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.