"use strict";
定义 JavaScript 代码应该在以下位置执行 “严格模式”。
"use strict"
指令是 ECMAScript 版本 5 中的新指令。
它不是一个语句,而是一个文字表达式,被早期版本忽略 JavaScript 的。
"use strict"
的目的是表明代码应该在“严格模式”下执行。
例如,在严格模式下,您不能使用未声明的变量。
除 Internet Explorer 9 及更低版本外,所有现代浏览器都支持“use strict”:
Directive | |||||
---|---|---|---|---|---|
"use strict" | 13.0 | 10.0 | 4.0 | 6.0 | 12.1 |
表中的数字指定完全支持该指令的第一个浏览器版本。
您可以在所有程序中使用严格模式。它可以帮助您编写更清晰的代码, 就像阻止您使用未声明的变量一样。
"use strict"
只是一个字符串,因此 IE 9 即使不理解它也不会抛出错误。
严格模式是通过在开头添加 "use strict"; 来声明的 脚本或函数。
在脚本的开头声明,它具有全局范围(所有代码 脚本中将以严格模式执行):
"use strict";
x = 3.14; // This will cause an error
because x is not declared
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Using a variable without declaring it, is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
x = 3.14; // This will cause an error (x is not defined).
</script>
</body>
</html>
"use strict";
myFunction();
function myFunction() {
y = 3.14; // This will also cause an error
because y is not declared
}
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>Global "use strict" declaration.</h2>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
myFunction();
function myFunction() {
y = 3.14; // This will cause an error (y is not defined)
}
</script>
</body>
</html>
在函数内部声明,它具有局部作用域(只有函数内部的代码是 在严格模式下):
x = 3.14; // This will not cause an error.
myFunction();
function
myFunction() {
"use strict";
y = 3.14; // This will cause an error
}
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<p>"use strict" in a function will only cause errors in that function.</p>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
x = 3.14; // This will not cause an error.
myFunction();
function myFunction() {
"use strict";
y = 3.14; // This will cause an error (y is not defined).
}
</script>
</body>
</html>
用于声明严格模式的语法被设计为兼容 旧版本的 JavaScript。
在 a 中编译数字文字 (4 + 5;) 或字符串文字 ("John Doe";) JavaScript 程序没有副作用。它只是编译为一个不存在的 变量并死亡。
因此 "use strict";
仅对“理解”含义的新编译器有意义 它的。
严格模式使编写“安全”JavaScript 变得更加容易。
严格模式将以前接受的“错误语法”更改为真正的错误。
举个例子,在普通的 JavaScript 中,错误输入变量名会创建一个新的变量 全局变量。在严格模式下,这会抛出错误,使其不可能 意外地创建了一个全局变量。
在正常的 JavaScript 中,开发者不会收到任何错误反馈 为不可写属性赋值。
在严格模式下,对不可写属性(仅限 getter)的任何赋值 属性、不存在的属性、不存在的变量或不存在的 对象,会抛出错误。
不允许使用变量而不声明它:
"use strict";
x = 3.14; // This will cause an error
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Using a variable without declaring it, is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
x = 3.14; // This will cause an error (x is not defined).
</script>
</body>
</html>
对象也是变量。
不允许使用未声明的对象:
"use strict";
x = {p1:10, p2:20}; // This will cause an error
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Using an object without declaring it, is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
x = {p1:10, p2:20}; // This will cause an error (x is not defined).
</script>
</body>
</html>
不允许删除变量(或对象)。
"use strict";
let x = 3.14;
delete x; // This
will cause an error
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Deleting a variable (or object) is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let x = 3.14;
delete x; // This will cause an error
</script>
</body>
</html>
不允许删除函数。
"use strict";
function x(p1, p2) {};
delete x;
// This will cause an error
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Deleting a function is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
function x(p1, p2) {};
delete x; // This will cause an error
</script>
</body>
</html>
不允许重复参数名称:
"use strict";
function x(p1, p1) {}; // This will cause an error
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Duplicating a parameter name is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
function x(p1, p1) {}; // This will cause an error
</script>
</body>
</html>
不允许使用八进制数字文字:
"use strict";
let x = 010; // This
will cause an error
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Octal numeric literals are not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let x = 010; // This will cause an error
</script>
</body>
</html>
不允许使用八进制转义字符:
"use strict";
let x = "\010"; // This will cause an error
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Octal escape characters are not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let x = "\010"; // This will cause an error
</script>
</body>
</html>
不允许写入只读属性:
"use strict";
const obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});
obj.x = 3.14; // This
will cause an error
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Writing to a read-only property is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
const obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});
obj.x = 3.14; // This will cause an error
</script>
</body>
</html>
不允许写入仅获取属性:
"use strict";
const obj = {get x()
{return 0} };
obj.x = 3.14; // This
will cause an error
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Writing to a get-only property is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
const obj = {get x() {return 0} };
obj.x = 3.14; // This will cause an error
</script>
</body>
</html>
不允许删除不可删除的属性:
"use strict";
delete Object.prototype; // This will cause an error
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Deleting an udeletable property is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
delete Object.prototype; // This will cause an error
</script>
</body>
</html>
单词 eval
不能用作变量:
"use strict";
let eval = 3.14; // This will cause an error
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>The string "eval" cannot be used as a variable.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let eval = 3.14; // This will cause an error
</script>
</body>
</html>
单词 arguments
不能用作变量:
"use strict";
let arguments = 3.14; // This will cause an error
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>The string "arguments" cannot be used as a variable.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let arguments = 3.14; // This will cause an error
</script>
</body>
</html>
不允许使用 with
语句:
"use strict";
with (Math){x = cos(2)}; // This will cause an error
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>The with statement is not allowed.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
with (Math){x = cos(2)}; // This will cause an error
</script>
</body>
</html>
出于安全原因,不允许 eval()
创建 调用它的范围内的变量。
在严格模式下,变量在声明之前不能使用:
"use strict";
eval ("x = 2");
alert (x); // This
will cause an error
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>For security reasons, eval() is not allowed to create variables in the scope from which it was called.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
eval ("x = 2");
alert (x); // This will cause an error
</script>
</body>
</html>
在严格模式下,eval() 不能使用 var 关键字声明变量:
"use strict";
eval ("var x = 2");
alert (x); // This
will cause an error
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>For security reasons, eval() is not allowed to create variables in the scope from which it was called.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
eval ("var x = 2");
alert (x); // This will cause an error
</script>
</body>
</html>
eval() 不能使用 let 关键字声明变量:
eval ("let x = 2");
alert (x); // This
will cause an error
自己尝试一下 →
<!DOCTYPE html>
<html>
<body>
<h2>Using eval()</h2>
<h3>For security reasons, eval() is not allowed to create variables in the scope from which it was called.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
eval ("let x = 2");
alert (x); // This will cause an error
</script>
</body>
</html>
函数中的 this
关键字的行为 在严格模式下有所不同。
this
关键字指的是 称为函数。
如果未指定对象,则以严格模式运行 将返回 undefined
并正常运行 模式将返回全局对象(窗口):
"use strict";
function myFunction() {
alert(this); // will alert "undefined"
}
myFunction();
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Inside functions, the "this" keyword is no longer the global object if not specified:</h3>
<script>
"use strict";
function myFunction() {
alert(this);
}
myFunction();
</script>
</body>
</html>
为未来 JavaScript 版本保留的关键字不能用作变量 严格模式下的名称。
这些都是:
"use strict";
let public = 1500; // This will cause an error
自己尝试一下→
<!DOCTYPE html>
<html>
<body>
<h2>With "use strict":</h2>
<h3>Future reserved keywords are not allowed in strict mode.</h3>
<p>Activate debugging in your browser (F12) to see the error report.</p>
<script>
"use strict";
let public = 1500; // This will cause an error
</script>
</body>
</html>
“use strict”指令仅在脚本的开头被识别 或一个函数。