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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:36:05+00:00 2026-06-06T21:36:05+00:00

Recently, while reading a very good book; ‘Maintainable Javascript’ , I discovered the use

  • 0

Recently, while reading a very good book; ‘Maintainable Javascript’, I discovered the use of “use strict” pragma.

The “use strict” seems to be a bad practice if it is declared in the global scope. The recommended way is to use the strict mode directly in each function like this:

// Non-strict code...

(function(){
  "use strict";

  // Define your library strictly...
})();

// Non-strict code...

Is it possible to define the strict mode to an entire namespace instead of defining it in each function? If yes, can I have one or two samples of code?

Thank you.

  • 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-06T21:36:06+00:00Added an answer on June 6, 2026 at 9:36 pm

    Strict mode applies to the execution context in which it’s declared and all of the contexts that context contains (with some squidginess around contexts created via eval, but avoid using eval and you avoid the squidginess), so usually you’d use the module pattern to apply it to all your code:

    (function() {
        "use strict";
    
        function foo() {
        }
    
        function bar() {
        }
    
        // ...presumably code hooking up `foo` and `bar` to events
        // or otherwise using them -- or of course, you could export
        // them from the scope...
    })();
    

    In the above, strict mode applies not only to the anonymous function, but to foo and bar as well. So for instance, where this code would “work” (create a global variable via The Horror of Implicit Globals):

    (function() {
        function foo() {
            someNameThatIsntDefined = 42; // Blech, creates implicit global
        }
    
        foo();
    })();
    

    …this code fails with a ReferenceError (the only change is the "use strict"):

    (function() {
        "use strict";
    
        function foo() {
            someNameThatIsntDefined = 42; // Throws ReferenceError
        }
    
        foo();
    })();
    

    …because one of the many useful things that strict mode does is get rid of the horror of implicit globals.

    Here’s another example, where we export a function that runs in strict mode even when called from non-strict code:

    var MyModule;
    MyModule = MyModule || {};
    (function(mod) {
        "use strict";
    
        mod.doSomethingUseful = doSomethingUseful;
        function doSomethingUseful() {
            // ...
        }
    
    })(MyModule);
    

    “Loose” code can call MyModule.doSomethingUseful, which always runs in strict mode. The upshot being that you can apply strict mode to your code without requiring that everyone using your code also use it. Very handy, that.

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

Sidebar

Related Questions

Recently, while reading a Socket Programming HOWTO the following section jumped out at me:
I recently stumbled upon Play framework while evaluating frameworks to use in a project.
Recently, I use Vim as my editor while programming Ruby, but I don't know
I've been searching and reading up on SignalR recently and, while I see a
recently, while reading former's code in my current project, I encounter the problems below:
Possible Duplicate: Weird Java Boxing Recently while I was reading about wrapper classes I
I've recently started reading up on Rails and while getting my development environment ready
I have recently started reading Programming Challenges book by S. Skiena and believe or
Recently I have been reading The Art Of Unit Testing book by Roy Osherove
I've been getting an error recently while debugging an ASP.NET application in Visual Studio

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.