|
| 1 | +<!--- |
| 2 | +{ |
| 3 | + "titles": [ |
| 4 | + "Use the function form of 'use strict'", |
| 5 | + "W097" |
| 6 | + ], |
| 7 | + "slugs": [ |
| 8 | + "use-the-function-form-of-use-strict", |
| 9 | + "w097" |
| 10 | + ], |
| 11 | + "linters": [ |
| 12 | + "jslint", |
| 13 | + "jshint", |
| 14 | + "eslint" |
| 15 | + ], |
| 16 | + "author": "jallardice" |
| 17 | +} |
| 18 | +--> |
| 19 | + |
| 20 | +### When do I get this error? |
| 21 | + |
| 22 | +The "Use the function form of 'use strict'" error is thrown when JSLint, JSHint |
| 23 | +or ESLint encounters a **strict mode directive in the outermost scope of the |
| 24 | +code**. In the following example we use a strict mode directive in the global |
| 25 | +scope to ensure the entire program runs in strict mode: |
| 26 | + |
| 27 | +<!--- |
| 28 | +{ |
| 29 | + "linter": "jslint" |
| 30 | +} |
| 31 | +--> |
| 32 | +```javascript |
| 33 | +"use strict"; |
| 34 | +function example() { |
| 35 | + return true; |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### Why do I get this error? |
| 40 | + |
| 41 | +This error is raised to highlight a **potentially dangerous piece of code**. |
| 42 | +It's common and good practice to concatenate multiple JavaScript files into one |
| 43 | +to reduce HTTP requests in production environments. If one of those files has a |
| 44 | +global strict mode directive and others do not, the concatenation would result |
| 45 | +in all scripts running in strict mode. If one the those scripts depends upon |
| 46 | +features that are disallowed in strict mode you may run into errors. Consider |
| 47 | +the following example which shows the previous script concatentated with another |
| 48 | +that relies upon features that are illegal in strict mode: |
| 49 | + |
| 50 | +<!--- |
| 51 | +{ |
| 52 | + "linter": "jslint" |
| 53 | +} |
| 54 | +--> |
| 55 | +```javascript |
| 56 | +"use strict"; |
| 57 | +function example() { |
| 58 | + return true; |
| 59 | +} |
| 60 | +function another(a) { |
| 61 | + return 010; // Octal literal, illegal in strict mode |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +This example will cause a syntax error since octal literals are not allowed in |
| 66 | +strict mode. If we want to use strict mode in our script and still be able to |
| 67 | +concatenate it with others we need to ensure our strict mode directive is not in |
| 68 | +the global scope. Since JavaScript only has function scope this means we need to |
| 69 | +place the directive within a function: |
| 70 | + |
| 71 | +<!--- |
| 72 | +{ |
| 73 | + "linter": "jslint" |
| 74 | +} |
| 75 | +--> |
| 76 | +```javascript |
| 77 | +function example() { |
| 78 | + "use strict"; |
| 79 | + return true; |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +This has the added benefit of allowing you to control exactly which parts of |
| 84 | +your own script run in strict mode. However, a common technique is to wrap your |
| 85 | +entire program in an immediately invoked function expression to constrain it to |
| 86 | +its own function scope and help prevent pollution of the global scope. |
| 87 | + |
| 88 | +In JSHint 1.0.0 and above you have the ability to ignore any warning with a |
| 89 | +[special option syntax][jshintopts]. The identifier of this warning is **W069**. |
| 90 | +This means you can tell JSHint to not issue this warning with the `/*jshint |
| 91 | +-W097 */` directive. You can also set the `sub` option to `true`. |
| 92 | + |
| 93 | +In ESLint the rule that generates this warning is named `no-global-strict`. You |
| 94 | +can disable it by setting it to `0`, or enable it by setting it to `1`. |
| 95 | + |
| 96 | +[jshintopts]: http://jshint.com/docs/#options |
0 commit comments