Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strict mode in a namespace with Javascript

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.

like image 559
Samuel Avatar asked Jul 02 '12 12:07

Samuel


People also ask

What is Strick mode in JavaScript?

Strict mode prohibits function statements that are not at the top level of a script or function. In normal mode in browsers, function statements are permitted "everywhere".

Where should we place strict in JavaScript?

We can also apply this to functions. To do this, we add the statement "use strict" or 'use strict' inside the functions on the top of the body, before any other statements. It's applied to everything inside, including functions that are nested in the function that uses strict mode.

How do I make JavaScript strict?

Using Strict mode for a function: Likewise, to invoke strict mode for a function, put the exact statement “use strict”; (or 'use strict';) in the function's body before any other statements. Examples of using Strict mode: Example: In normal JavaScript, mistyping a variable name creates a new global variable.


1 Answers

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.

like image 82
T.J. Crowder Avatar answered Sep 21 '22 13:09

T.J. Crowder