严格模式和保留字

Strict mode and reserved word

为什么这段代码没问题:

var test = {
    fn1: function(_origin, _componentType) {
        if(arguments.length > 1) throw "xx";
        // this strict is ok
        "use strict";

        var interface               = new Object(this);
    }
}

虽然这不是

var test = {
    fn1: function(_origin, _componentType) {
        // This strict throws SyntaxError
        "use strict";

        if(arguments.length > 1) throw "xx";
        var interface               = new Object(this);
    }
}

我知道接口在严格模式下是保留字,但两个例子不应该都抛出错误吗?

"use strict"; 需要是函数(或脚本,如果是脚本范围)中的第一条语句才能触发严格模式;在其他任何地方,您也可以写 "merry christmas";.

第一个示例实际上并未启用严格模式。见 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#Invoking_strict_mode:

Strict mode applies to entire scripts or to individual functions. It doesn't apply to block statements enclosed in {} braces; attempting to apply it to such contexts does nothing.