首页 > 技术文章 > ECMAScript5严格模式

zhaodongyu 2013-08-12 19:44 原文

ECMAScript5引入了严格模式(strict mode)的概念,IE10+开始支持。严格模式为JavaScript定义了一种不同的解析和执行模型,在严格模式下,ECMAScript3中的一些不确定或不安全的行为将会抛出一些错误。

开启严格模式的方法是在“作用域”的第一行加上

'use strict';

在严格模式下,未使用var定义的全局变量会报错,这是检验当前域是否为严格模式的一种简单方法。

函数级作用域:

// 严格模式
(function foo() {
  'use strict';
  bar = 10; // ReferenceError
})();

脚本级作用域:

<script>
    // 严格模式
    'use strict';
    foo = 10; // ReferenceError
</script>

脚本级别的严格模式不影响该页面的另一个脚本。

严格模式带来的变化

在严格模式下,ECMAScript3中的一些“失误”会抛出“错误”,同时它去除了一些容易产生问题语法,这使得调试错误变得更为直接。

无意(未使用var)创建一个全局变量会报错

'use strict';

// Uncaught ReferenceError: 
// mistypedVariable is not defined 
mistypedVariable = 17;

重复性检查

'use strict';

// Uncaught SyntaxError: 
// Duplicate data property in object literal not allowed in strict mode 
var o = { p: 1, p: 2 }; 

// Uncaught SyntaxError: 
// Strict mode function may not have duplicate parameter names 
function sum(a, a, c){
    return a + b + c; 
}

函数中的this关键字

在严格模式中,函数顶层的this将不再指向window,而是undefined。

'use strict';
function foo(){
    console.log(this) // undefined
}
foo();

禁止删除变量

'use strict';
var x;

// Uncaught SyntaxError:
// Delete of an unqualified identifier in strict mode. 
delete x;

禁止定义八进制字面量

在《JavaScript高级程序设计(第三版)》中提到“八进制字面量在严格模式下是无效的...”,这里存在一个小小的问题:何为字面量?

You use literals to represent values in JavaScript. These are fixed values, not variables, that you literally provide in your script.

——Values, variables, and literals

比如数组的字面量[],比如对象的字面量{},再比如整数的字面量有以下三种:

  • 0, 117 and -345 (decimal, base 10)
  • 015, 0001 and -077 (octal, base 8) 
  • 0x1123, 0x00111 and -0xF1A7 (hexadecimal, "hex" or base 16)

所以这样的字面量写法在严格模式下显然不可以:

'use strict';

// Uncaught SyntaxError: 
// Octal literals are not allowed in strict mode.
var foo = 063; 

既然书里说八进制字面量行不通,那么好奇心驱使我试验了整数字面量的引用类型new Number(063),在我的印象里,它和字面量是有区别的。

console.log(typeof 1, typeof new Number(1)); // number object

但是,

'use strict';

// Uncaught SyntaxError:
// Octal literals are not allowed in strict mode
var foo = new Number(063);

所以,在严格模式下,字面量方式和引用方式都不能显示的定一个八进制变量。

禁止使用with语句

使用with语句会引发一些意想不到的问题,比如

var root = {
    branch: {
        node: 1
    }
};
 
with(root.branch) {
    root.branch = {
        node: 0
    };
    // 显示 1, 错误!
    alert(node);
}
// 显示 0, 正确!
alert(root.branch.node);

所以严格模式下去除了with语句,消除动态作用域带来的负面效果。

'use strict';

// Uncaught SyntaxError: 
// Strict mode code may not include a with statement
with (obj){
    
}

eval作用域

同样,禁止在eval中声明变量,也是在严格模式下保证了静态作用域。

'use strict';
eval('var foo = 2'); 

// Uncaught ReferenceError:
// bar is not defined 
console.log(foo);

在函数中不可访问caller、callee以及arguments

function foo(){
  'use strict';
  foo.caller;    // TypeError
  foo.arguments; // TypeError
  arguments.callee; // TypeError
}
foo();

必须在脚本或者函数的最上层声明函数

'use strict';

// Uncaught SyntaxError: 
// In strict mode code, 
// functions can only be declared at top level or
//  immediately within another function. 
if (true){
    function f1(){ }
    f1();
}

for (var i = 0; i &lt; 5; i++){
    function f2(){ } // SyntaxError
    f2();
}

增加一些保留字

严格模式新增了一些保留字:implements、 interface、 let、 package、 private、 protected、 public、 static、 yield,使用这些词作为变量会报错。

 

结语:严格模式使得JavaScript表现的更为严谨,让开发者不必纠结于分析作用域的动态结果,也减少了由于一些潜在的bug引起的异常结果。

推荐阅读