Home > Web Front-end > JS Tutorial > Introduction to ECMAScript 5 Strict Mode_Basic Knowledge

Introduction to ECMAScript 5 Strict Mode_Basic Knowledge

WBOY
Release: 2016-05-16 16:11:42
Original
1351 people have browsed it

Strict Mode is a new feature of ECMAScript 5, which allows you to place the entire program, or a certain function, in a "strict" operating context. This strict context will prevent certain operations and throw more exceptions.

Although ECMAScript 5 is backwards compatible with ECMAScript 3, in strict mode all features deprecated in ECMAScript 3 are disabled (or throw errors) instead of being compatible.

Enabling strict mode has the following benefits:

1. Catch some programming errors and throw exceptions.
2. Prevent some relatively "unsafe" operations (such as accessing global variables) and throw exceptions.
3. Disable some confusing features.

Most information about strict mode can be found on page 223 of the ES5 Specification [PDF].

(Note: ECMAScript 5’s strict mode is different from Firefox’s strict mode)

How to enable strict mode

Add this statement at the beginning of the program to enable strict mode for the entire script:

Copy code The code is as follows:

'use strict';

You can also enable strict mode only inside the function, so that it will not affect the outside:
Copy code The code is as follows:

function imStrict() {
'use strict';
​//...your code...
}

The statement that enables strict mode is just a plain string "use strict" without any new syntax. This means there will be no negative impact on older browsers.

A practical application of enabling strict mode inside a function is to define the entire Javascript class library inside a strict mode function, so that it does not affect external code:

Copy code The code is as follows:

// Non-strict code...

(function(){
"use strict";

// Define your library strictly...
})();

// Non-strict code...


So, what changes to scripts in strict mode?

Variables and Properties

Assignment to an undefined variable will fail instead of making the variable a global variable.

Writing a property with a writable property of false, deleting a property with a configurable property of false, or adding an extensible property with a false property will result in an error (these properties are pre-agreed). In the past, these operations did not throw an exception and simply failed silently.

Performing a delete operation on a variable, function or function parameter will cause an error.

Copy code The code is as follows:

var foo = 'test';
function test() { }

delete foo; // Error
delete test; // Error

function test2(arg) {
delete arg; // Error
}


Defining the same properties inside an object container will cause an exception to be thrown:
Copy code The code is as follows:

//Error
{ foo: true, foo: false }

eval

Any use of the name "eval" (the main intention is to point the eval function to a variable or object property) is prohibited.

Copy code The code is as follows:

// All generate errors...
obj.eval = ...
obj.foo = eval;
var eval = ...;
for ( var eval in ... ) {}
function eval(){}
function test(eval){}
function(eval){}
new Function("eval")

Additionally, declaring new variables via eval will also have no effect:
Copy code The code is as follows:

eval("var a = false;");
print( typeof a ); // undefined

Function

Overriding the arguments object results in an error:

Copy code The code is as follows:

arguments = [...]; // not allowed

Parameters with the same name will cause an error:
Copy code The code is as follows:

(function(foo, foo) { }) // Error

Access to arguments.caller and arguments.callee will throw an exception. Therefore, any anonymous function that needs to be used must be named first, for example:
Copy code The code is as follows:

setTimeout(function later(){
// do stuff...
​setTimeout(later, 1000);
}, 1000 );

The function's arguments, caller, and callee properties no longer exist, and the operations that defined them are prohibited.
Copy code The code is as follows:

function test() { }
test.caller = 'caller'; // Error

Finally, a long-standing (and very annoying) BUG has been solved: when using null or undefined as the first parameter of the Function.prototype.call or Function.prototype.apply method, this inside the function will point to the global object. Strict mode will prevent its execution and throw an exception:
Copy code The code is as follows:

(function(){ ... }).call(null); // Exception

with() { }

with() { } statement is completely broken in strict mode.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template