ES6 : Strict Mode - wesleyduff/Blog-and-Tutorials GitHub Wiki
#Strict Mode
Strict mode was introduced in ECMAScript 5 to clean up the language. Make sure to feature-test for strict mode if you want to use it. It is switched on by adding by putting the following line first in a file or in a function
'use strict';
##Facts
- Intentionally changes semantics for normal code.
- Browsers not supporting strict mode will run strict mode code with different behavior from browsers that do support strict mode.
- Eliminates some JavaScript silent errors by changing them to throw errors.
- Fixes mistakes that make it difficult for JavaScript engines to perform optimizations.
- Can sometimes be made to run faster than identical code that's not strict mode.
- Prohibits some syntax likely to be defined in future versions of ECMAScript.
###Breaking changes:
- with statement is forbidden. The "with" statement lets users add arbitrary objects to the chain of variable scopes, which slows down execution and makes it tricky to figure out what a variable refers to.
- Deleting a variable is forbidden.
- Functions can only be declared at the top level of a scope.
- More keywords are reserved :
- implements
- interface
- let
- package
- private
- protected
- public
- static
- yield
###More Errors
- Assigning to an undeclared variable causes a ReferenceError.
- In non-strict mode the variable becomes a global variable.
'use strict';
mystring = "test";
//Uncaught ReferenceError: mystring is not defined
- Changing read-only properties cause a TypeError. In non-strict mode, it has no effects.
'use strict';
var myString = "This is a length of a string";
myString.length = 3;
//Uncaught TypeError: Cannot assign to read only property 'length' of string 'This is a length of a string'