Prefer using strict mode in the global scope.
2// This file (and *only* this file) is released under the ISC license:
3// https://opensource.org/licenses/ISC
4;(function() {
5 'use strict'; 6
7 if (window.goatcounter && window.goatcounter.vars) // Compatibility with very old version; do not use.
8 window.goatcounter = window.goatcounter.vars
Description
A strict mode directive is a "use strict"
literal at the beginning of a script or function body. It enables strict mode semantics.
When a directive occurs in global scope, strict mode applies to the entire script:
"use strict";
// strict mode
function foo() {
// strict mode
}
When a directive occurs at the beginning of a function body, strict mode applies only to that function, including all contained functions:
function foo() {
"use strict";
// strict mode
}
function foo2() {
// not strict mode
};
(function() {
"use strict";
function bar() {
// strict mode
}
}());
In the CommonJS module system, a hidden function wraps each module and limits the scope of a "global" strict mode directive.
In ECMAScript modules, which always have strict mode semantics, the directives are unnecessary.
Bad Practice
function foo() {
}
function foo() {
"use strict";
}
"use strict";
function foo() {
"use strict";
}
Recommended
function foo() {
}