"use strict"
指令在 JavaScript 1.8.5 (ECMAScript5) 中新增。
"use strict"
的目的是指定代码在严格条件下执行。
严格模式下你不能使用未声明的变量。
"use strict";
x = 3.14;// 报错 (x 未定义)
"use strict";
myFunction();
function myFunction() {
y = 3.14; // 报错 (y 未定义)
}
x = 3.14; // 不报错,因为作用域中没有use strict
myFunction();
function myFunction() {
"use strict";
y = 3.14; // 报错 (y 未定义),因为作用域中有use strict
}
为什么使用严格模式: