Use Strict - jellyfish-tom/TIL GitHub Wiki
[SOURCES]
- https://johnresig.com/blog/ecmascript-5-strict-mode-json-and-more/
- http://2ality.com/2011/01/javascripts-strict-mode-summary.html
Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a “strict” operating context. This strict context prevents certain actions from being taken and throws more exceptions (generally providing the user with more information and a tapered-down coding experience).
Since ECMAScript 5 is backwards-compatible with ECMAScript 3, all of the “features” that were in ECMAScript 3 that were “deprecated” are just disabled (or throw errors) in strict mode, instead.
Strict mode helps out in a couple ways:
It catches some common coding bloopers, throwing exceptions. It prevents, or throws errors, when relatively “unsafe” actions are taken (such as gaining access to the global object). It disables features that are confusing or poorly thought out.
Most important changes it delivers:
- Improved safety: very limited use of eval(), with statement not permitted.
- Global variables must be explicitly declared (no more auto-creation). This helps to prevent typos.
- Calling constructor functions without new: Prior to strict mode, this was bound to the global object which resulted in properties being added to that object. In strict mode, this will be bound to undefined and an exception will usually be thrown if constructors are called without new.
- Noisy failure: Attempting to change a read-only property throws an exception. So does attempting to delete a non-configurable property.
- No more octal numbers: As a remnant from JavaScript’s C inheritance, numbers with leading zeros were interpreted as octal. Now 0100 really is 100 and not 64. And 08 is not an error, any more.
- Arguments object: The properties arguments.callee and arguments.caller have been eliminated (for safety reasons: it keeps them secret from foreign code).
- Function parameters: No more duplicate parameter names or variables that have the same name as a parameter.