Strict mode in JavaScript, also known as strict mode, runs under strict conditions. It eliminates some unreasonable and rigorous aspects of JavaScript syntax, reduces some weird behaviors, eliminates some unsafe aspects of code execution, ensures code execution safety, improves engine efficiency, increases running speed, and lays the foundation for future new JavaScript versions.
// Strict mode"use strict";var y =010;// Uncaught SyntaxError: Octal literals are not allowed in strict mode.var x =0O10;// Can use the new syntax of octal representation in ES6, prefix 0o or 0Oconsole.log(x);// 8
// Non-strict mode// Operation silently fails, meaning no error is raised and no effect takes place// Assign a value to a non-writable propertyvar obj ={};Object.defineProperty(obj,"x",{value:0,writable:false});obj.x =1;console.log(obj.x);// 0// Assign a value to a read-only propertyvar obj ={_x:0,getx(){returnthis._x;}};obj.x =1;console.log(obj.x);// 0// Assign a value to a new property of a non-extensible objectvar obj ={};Object.preventExtensions(obj);obj.x =1;console.log(obj.x);// undefined
// Strict mode// Operation fails and throws an exception"use strict";// Assign a value to a non-writable propertyvar obj ={};Object.defineProperty(obj,"x",{value:0,writable:false});obj.x =1;// Uncaught TypeError: Cannot assign to read only property 'x' of object '#<Object>'// Assign a value to a read-only propertyvar obj ={_x:0,getx(){returnthis._x;}};obj.x =1;// Uncaught TypeError: Cannot set property x of #<Object> which has only a getter// Assign a value to a new property of a non-extensible objectvar obj ={};Object.preventExtensions(obj);obj.x =1;// Uncaught TypeError: Cannot add property x, object is not extensible
Not allowed to name variables with reserved keywords#
In strict mode, the value passed to a function through this is not coerced into an object. For a regular function, this will always be an object: whether this is originally an object when called; when the function is called with a boolean value, a string, or a number, this inside the function will be encapsulated as an object; or when the function is called with undefined or null, this represents the global object (using the call, apply, or bind method to specify a specific this). This automatic conversion to an object is not only a performance loss but also exposes the global object in the browser, posing a security risk because the global object provides a way to access functions that a secure JavaScript environment must restrict. For a function in strict mode, the specified this is no longer encapsulated as an object, and if this is not specified, its value is undefined.
In strict mode, widely implemented ECMAScript extensions can no longer roam the stack of JavaScript. In normal mode, when using these extensions, when a function called fun is being called, fun.caller is the last function to call fun, and fun.arguments contains the formal parameters used in the call to fun. Both of these extension interfaces are problematic for secure JavaScript because they allow secure code to access privileged functions and their (often unprotected) formal parameters. If fun is in strict mode, then fun.caller and fun.arguments are both non-deletable properties and will result in an error when storing or accessing their values.
// Strict mode"use strict";functionss(){ console.log(ss.caller);// Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them console.log(ss.arguments);// Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them}functions(){ss();}s();