JavaScript Use Strict
- Enable strict mode
- Example of the strict mode for a global scope:
- Example of the strict mode for a functions:
- Example of the undeclared variable in strict mode:
- Example of the variable or object without declaring it:
- Example of the reserved words to declare variables:
- Example of the function’s undeclared variable in strict mode:
- Example of the strict mode is declared inside a function:
- Example of the deleting variables, object, functions in strict mode raises an error:
- Example of the function arguments can’t be deleted and have same name:
- “Strict mode” reports errors on the following:
On this page
- Enable strict mode
- Example of the strict mode for a global scope:
- Example of the strict mode for a functions:
- Example of the undeclared variable in strict mode:
- Example of the variable or object without declaring it:
- Example of the reserved words to declare variables:
- Example of the function’s undeclared variable in strict mode:
- Example of the strict mode is declared inside a function:
- Example of the deleting variables, object, functions in strict mode raises an error:
- Example of the function arguments can’t be deleted and have same name:
- “Strict mode” reports errors on the following:
JavaScript Use Strict
For a long time, JavaScript developed without any similarity issues. There were added some new attributes to the language, but old functionality didn’t change. That had the profit of never breaking existing code. But the negative aspect was that any mistake or some imperfect decision made by the creators of JavaScript got stuck in the language forever. (长期以来, JavaScript的开发没有任何相似性问题。 该语言添加了一些新属性,但旧功能没有改变。 这样做的好处是从不破坏现有的代码。 但消极的一面是, JavaScript的创建者所犯的任何错误或做出的某些不完美的决定都会永远停留在语言中。)
When ECMAScript 5 (ES5) appeared in 2009, it added new attributes to the language and modified old ones. For keeping the existing ones working, you need to enable them with command “use strict”, which forces a program to work under a “strict” operating context. (当ECMAScript 5 ( ES5 )在2009年出现时,它为语言添加了新的属性并修改了旧的属性。为了保持现有的工作,您需要使用“use strict”命令启用它们,这迫使程序在“严格”的操作环境中工作。)
The “use strict” directive is not a statement, but a literal expression, which was ignored by many earlier versions of JavaScript. The main aim of “use strict” is to indicate that the code should be executed in “strict mode”. Almost all modern browsers support “use strict”. When new browser sees the keyword “use strict” it turns itself into strict mode operating. (“use strict”指令不是一个语句,而是一个字面表达式,许多早期版本的JavaScript都忽略了这一点。“严格使用”的主要目的是表明代码应在“严格模式”下执行。几乎所有现代浏览器都支持“严格使用”。当新浏览器看到关键字“use strict”时,它将自身转变为严格模式操作。)
Enable strict mode
Enable strict mode (启用严格模式)
We declare strict mode after adding “use strict” to the beginning of a function or a script. When it declared at the beginning of a script, it has global scope, which means that all code in the script will execute in strict mode. (我们在函数或脚本的开头添加“use strict”后声明严格模式。当它在脚本开头声明时,它具有全局范围,这意味着脚本中的所有代码都将以严格模式执行。)
Example of the strict mode for a global scope:
//syntax global strict mode
'use strict';
var value = "This is strict mode script!";
console.log(value);
“use strict” can be put at the beginning of the function body instead of the whole script. (“use strict”可以放在函数体的开头,而不是整个脚本。)
Example of the strict mode for a functions:
// not strict
function strict() {
// strict mode syntax in function
(//函数中的严格模式语法)
'use strict';
return "This is strict mode script!";
}
// not strict
But usually, people use it for the whole script as well. (但通常情况下,人们也会将其用于整个剧本。)
Strict mode changes early accepted “bad syntax” into real errors. Below you can see the examples of the “use strict” with errors. (严格模式将早期接受的“错误语法”更改为实际错误。您可以在下面看到带有错误的“严格使用”示例。)
Example of the undeclared variable in strict mode:
"use strict";
x = 10; // causes an error because x is not declared
console.log(x);
Example of the variable or object without declaring it:
'use strict';
var myValue = 7;
myValu = 10; // this line throws a ReferenceError due to the
console.log(myValu);
// misspelling the variable
value = 20; // this line also throws a ReferenceError as variable is not declared
Example of the reserved words to declare variables:
var let = 10;
console.log(let) // output 10
"use strict";
var let = 10;
console.log(let) // raises an error
There are some keywords that can’t be used as variable names in strict mode: implements, interface, let, package, private, protected, public, static, yield.
Example of the function’s undeclared variable in strict mode:
"use strict";
w3Function();
function w3Function() {
x = 10; // causes an error because x is not declared
console.log(x);
}
When strict mode is declared inside a function, it has local scope. In this case only the code inside the function is in strict mode. (当在函数内声明严格模式时,它具有本地作用域。在这种情况下,只有函数内的代码处于严格模式。)
Example of the strict mode is declared inside a function:
x = 10; // doesn’t cause an error.
w3Function();
function w3Function() {
"use strict";
y = 10; // causes an error
console.log(y);
}
console.log(x);
Example of the deleting variables, object, functions in strict mode raises an error:
"use strict";
var x = 54;
var obj = {
'name': 'w3cdoc'
};
function f() {
console.log("w3cdoc");
}
delete x; //error, delete of an unqualified identifier in strict mode.
delete obj; //error, delete of an unqualified identifier in strict mode.
delete f; //error, delete of an unqualified identifier in strict mode.
Example of the function arguments can’t be deleted and have same name:
"use strict";
function f(a, b) {
console.log("w3cdoc");
delete(a); //error, delete of an unqualified identifier in strict mode.
}
"use strict"
function f(a, a) { // error, duplicate parameter name not allowed in this context
console.log("w3cdoc");
}
“Strict mode” reports errors on the following:
“Strict mode” reports errors on the following:
Using a variable, without declaring it. (-使用变量,而不声明它。)
Using an object, without declaring it. (-使用对象,无需声明。)
Using reserved keywords as variable names. (-使用保留关键字作为变量名称。)
Deleting a variable is not allowed. (-不允许删除变量。)
Deleting a function is not allowed. (-不允许删除函数。)
Duplicating a parameter name is not allowed. (-不允许复制参数名称。)
The word eval can’t be used as a variable. (- eval一词不能用作变量。)
For security reasons, there is not allowed for eval() to create variables in the scope from which it was called. (-出于安全原因, eval ()不允许在调用它的作用域中创建变量。)