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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:00:15+00:00 2026-05-16T00:00:15+00:00

I want to check in a script if a certain other module is already

  • 0

I want to check in a script if a certain other module is already loaded.

if (ModuleName) {
    // extend this module
}

But if ModuleName doesn’t exist, that throws.

If I knew what the Global Object was I could use that.

if (window.ModuleName) {
    // extend this module
}

But since I want my module to work with both browsers and node, rhino, etc., I can’t assume window.

As I understand it, this doesn’t work in ES 5 with "use strict";

var MyGLOBAL = (function () {return this;}()); // MyGlobal becomes null

This will also fail with a thrown exception

var MyGLOBAL = window || GLOBAL

So it seems like I’m left with

try {
    // Extend ModuleName
} 
catch(ignore) {
}

None of these cases will pass JSLint.

Am I missing anything?

  • 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-16T00:00:16+00:00Added an answer on May 16, 2026 at 12:00 am

    Well, you can use the typeof operator, and if the identifier doesn’t exist in any place of the scope chain, it will not throw a ReferenceError, it will just return "undefined":

    if (typeof ModuleName != 'undefined') {
      //...
    }
    

    Remember also that the this value on Global code, refers to the global object, meaning that if your if statement is on the global context, you can simply check this.ModuleName.

    About the (function () { return this; }()); technique, you are right, on strict mode the this value will simply be undefined.

    Under strict mode there are two ways to get a reference to the Global object, no matter where you are:

    • Through the Function constructor:

      var global = Function('return this')();
      

    Functions created with the Function constructor don’t inherit the strictness of the caller, they are strict only if they start their body with the 'use strict' directive, otherwise they are non-strict.

    This method is compatible with any ES3 implementation.

    • Through an indirect eval call, for example:

      "use strict";
      var get = eval;
      var global = get("this");
      

    The above will work because in ES5, indirect calls to eval, use the global environment as both, the variable environment and lexical environment for the eval code.

    See details on Entering Eval Code, Step 1.

    But be aware that the last solution will not work on ES3 implementations, because an indirect call to eval on ES3 will use the variable and lexical environments of the caller as the environments for the eval code itself.

    And at last, you may find useful to detect if strict mode is supported:

    var isStrictSupported = (function () { "use strict"; return !this; })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this script that collects data from users and I want to check
I want to write a bash function that check if a file has certain
So I have an upload script, and I want to check the file type
I want to write a batch script statement where: FINDSTR has to check for
I want to (need to) start a sub-process from a perl script that checks
I want to check checkbox like $('#riderCheck').attr('checked','true') on some condition. but the problem is
I want to check a particular file exist in Azure Blob Storage. Is it
I can't seem to figure this one out. I have a validator-type script that
I want to check if the users on our domain have a certain Firefox
So I have this basic script: /*check if email is valid*/ if (substr_compare (

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.