Java Reference
In-Depth Information
date = new Date();
today = days[date.getDay()];
console.log("Welcome back " + name + ". Today is " +
today);
})();
<< "Welcome back Bart. Today is Sunday"
Safe Use of Strict Mode
In the last chapter we discussed using strict mode to avoid any sloppy coding practices.
One of the problems with simply placing "use strict" at the beginning of a file is that
it will enforce strict mode on all the JavaScript in the file, and if you're using other people's
code, there's no guarantee that they've coded in strict mode.
To avoid this, the recommended way to use strict mode is to place all your code inside an
IIFE, like so:
(function() {
"use strict";
// All your code would go inside this function
}());
This ensures that only your code inside the IIFE is forced to use strict mode.
Creating Self-contained Modules
An IIFE can be used to enclose a block of code inside its own private scope. This effect-
ively creates a self-contained module that will not interfere with any other part of the pro-
gram. Using IIFEs in this way means that code can be added or removed in a modular fash-
ion. The example shows two modules, A and B, that are able to run code independently of
each other:
(function() {
// Module A
Search WWH ::




Custom Search