JavaScript 严格模式(use strict)

Author : luckyGirl

“use strict”在一些插件中是常见的,是ECMAScript 5的另一种运行模式 —— “严格模式”,即在这种模式下,对于JavaScript代码则更加严格,可以更好的检查代码的错误或是不严谨,因此同样的代码在“严格模式”下可能会有不同的运行效果。ECMAScript引入这一模式主要是对于以下几个因素的考虑:

  • 安全性,消除JS代码运行的不安全之处
  • 改善代码质量,避免易发的错误
  • 改善代码的错误检测

一、声明严格模式

使用”use strict”语句,代码将进入“严格模式”,使用的场合有文件,模块或是一个函数,无论哪种,这个声明必须放在第一行,否则无效。

整个文件处于严格模式

"use strict";
function testFunction(){
    var testvar = 4;
    return testvar;
}
// This causes a syntax error.
testvar = 5;

针对单个函数采用严格模式

function testFunction(){
    "use strict";
    // This causes a syntax error.
    testvar = 4;
    return testvar;
}
testvar = 5;

模块或是库,常常采用匿名函数自执行,则自执行函数内部采用严格模式

+function ($) {
  'use strict';
  // Define your code hre
}();

二、严格模式下的语法限制

在严格模式下,JS的语法以及运行结果产生了一些改变,之前在正常模式下能运行的函数或仅是执行失败的语法,在严格模式下会报错

变量使用前必须声明

textvar = 4; //testvar is not defined 

Error: SCRIPT5042: Variable undefined in strict mode

只读属性不能赋值

 var testObj = Object.defineProperties({}, {
    prop1: {
        value: 10,
        writable: false // by default
    },
    prop2: {
        get: function () {
        }
    }
});
testObj.prop1 = 20; //Cannot assign to read only property 'prop1' of #<Object> 
testObj.prop2 = 30; //Cannot set property prop2 of #<Object> which has only a getter 

Error: SCRIPT5045: Assignment to read-only properties is not allowed in strict mode.

禁止对禁止扩展的对象添加属性

var testObj = new Object();
Object.preventExtensions(testObj);
testObj.name = "Bob"; //Can't add property name, object is not extensible 

Error: SCRIPT5046: Cannot create property for a non-extensible object.

禁止删除变量,函数或者是configurable值为false的属性

 var testvar = 15;
function testFunc() {};
delete testvar;
delete testFunc;
Object.defineProperty(testObj, "testvar", {
    value: 10,
    configurable: false
    });
delete testObj.testvar;//Delete of an unqualified identifier in strict mode. 

Error: SCRIPT1045: Calling delete on is not allowed in strict mode.

对象不能有重名的属性

 var testObj = {
    prop1: 10,
    prop2: 15,
    prop1: 20
};

Error:SCRIPT1046: Multiple definitions of a property not allowed in strict mode

函数不能有重名的参数

 function testFunc(param1, param1) {
    return 1;
};

Error: SCRIPT1038: Duplicate formal parameter names not allowed in strict mode

新增了一些保留字

implements
interface
package
private
protected
public
static
yield

SCRIPT1050: The use of a future reserved word for an identifier is invalid. The identifier name is reserved in strict mode.

禁止使用八进制

var testoctal = 010;
var testescape = \010;

SCRIPT1039: Octal numeric literals and escape characters not allowed in strict mode

禁止this关键字指向全局对象

function testFunc() {
    return this;
}
var testvar = testFunc(); //undefined

禁止用eval作为标识符(变量,函数名,参数名等等)

var eval = 10; 

函数需要声明在顶层

var arr = [1, 2, 3, 4, 5];
var index = null;
for (index in arr) {
    function myFunc() {};
}

SCRIPT1047: In strict mode, function declarations cannot be nested inside a statement or block. They may only appear at the top level or directly inside a function body.

添加eval作用域

在eval内部声明的变量无法在外部访问

var indirectEval = eval;
indirectEval("var testvar = 10;");
document.write(testVar);

SCRIPT5009: ‘testVar’ is undefined.

arguments不能作为标识符(变量,函数名,参数名等)

var arguments = 10;

SCRIPT1042: Invalid usage of ‘arguments’ in strict mode

arguments,不能通过改变arguments对象修改参数值

function testArgs(oneArg) {
    arguments[0] = 20;
}

在正常模式下,oneArg与arguments[0]均为20,但是在严格模式下,oneArg值并没有改变,因为arguments队形仅仅是本地的副本

禁用arguments.callee

    function aa(testInt) {
    if (testInt-- == 0)
        return;
    arguments.callee(testInt--);
};
aa();

Uncaught TypeError: ‘caller’, ‘callee’, and ‘arguments’ properties may not be accessed on strict mode functions or the arguments objects for calls to them

禁用 with

with (Math){
    x = cos(3);
    y = tan(7);
}
//SCRIPT1037: 'with' statements are not allowed in strict mode.

Reference:Internet Explorer Dev Center

standard

Have your say