Strict Mode

Strict mode is a way to introduce better error-checking into your code. When you use strict mode, you cannot, for example, use implicitly declared variables, or assign a value to a read-only property, or add a property to an object that is not extensible. The restrictions are listed in the Restrictions on Code in Strict Mode section later in this topic.

Caution note Caution

Strict mode is not supported in versions of Internet Explorer earlier than Internet Explorer 10.

You can declare strict mode by adding "use strict"; at the beginning of a file, a program, or a function. This kind of declaration is known as a directive prologue. The scope of a strict mode declaration depends on its context. If it is declared in a global context (outside the scope of a function), all the code in the program is in strict mode. If it is declared in a function, all the code in the function is in strict mode. For example, in the following example all the code is in strict mode, and the variable declaration outside the function causes the syntax error "Variable undefined in strict mode."

JavaScript
"use strict";
function testFunction(){
    var testvar = 4;
    return testvar;
}

// This causes a syntax error.
testvar = 5;

In the following example, only the code inside testFunction is in strict mode. The variable declaration outside the function does not cause a syntax error, but the declaration inside the function does.

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

The following table lists the most important restrictions that apply in strict mode.

Language element

Restriction

Error

Example

Variable

Using a variable without declaring it.

SCRIPT5042: Variable undefined in strict mode

JavaScript
testvar = 4;

Read-only property

Writing to a read-only property.

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

JavaScript
var testObj = Object.defineProperties({}, {
    prop1: {
        value: 10,
        writable: false // by default
    },
    prop2: {
        get: function () {
        }
    }
});
testObj.prop1 = 20; 
testObj.prop2 = 30;

Non-extensible property

Adding a property to an object whoseextensibleattribute is set tofalse.

SCRIPT5046: Cannot create property for a non-extensible object

JavaScript
var testObj = new Object();

Object.preventExtensions(testObj);

testObj.name = "Bob";

delete

Deleting a variable, a function, or an argument.

Deleting a property whoseconfigurableattribute is set tofalse.

SCRIPT1045: Calling delete on is not allowed in strict mode

JavaScript
var testvar = 15;
function testFunc() {};
delete testvar;
delete testFunc;

Object.defineProperty(testObj, "testvar", {
    value: 10,
    configurable: false
    });
delete testObj.testvar;

Duplicating a property

Defining a property more than once in an object literal.

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

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


Duplicating a parameter name

Using a parameter name more than once in a function.

SCRIPT1038: Duplicate formal parameter names not allowed in strict mode

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

Future reserved keywords

Using a future reserved keyword as a variable or function name.

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

  • implements

  • interface

  • let

  • package

  • private

  • protected

  • public

  • static

  • yield

Octals

Assigning an octal value to a numeric literal, or attempting to use an escape on an octal value.

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

JavaScript
var testoctal = 010;
var testescape = \010;

this

The value of thisis not converted to the global object when it isnull orundefined.

 

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

In non-strict mode, the value of testvar is the global object, but in strict mode the value is undefined.

eval as an identifier

The string "eval" cannot be used as an identifier (variable or function name, parameter name, and so on).

 

JavaScript
var eval = 10;

Function declared inside a statement or a block

You cannot declare a function inside a statement or a block.

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.

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

Variable declared inside an evalfunction

If a variable is declared inside an evalfunction, it cannot be used outside that function.

SCRIPT1041: Invalid usage of 'eval' in strict mode

JavaScript
eval_r("var testvar = 10");
testvar = 15;

Indirect evaluation is possible, but you still cannot use a variable declared outside the eval function.

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

This code causes an error SCRIPT5009: 'testVar' is undefined.

Arguments as an identifier

The string "arguments" cannot be used as an identifier (variable or function name, parameter name, and so on).

SCRIPT1042: Invalid usage of 'arguments' in strict mode

JavaScript
var arguments = 10;

arguments inside a function

You cannot change the values of members of the local argumentsobject.

 

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

In non-strict mode, you can change the value of the oneArgparameter by changing the value of arguments[0], so that the value of both oneArg and arguments[0] is 20. In strict mode, changing the value of arguments[0] does not affect the value ofoneArg, because the arguments object is merely a local copy.

arguments.callee

Not allowed.

 

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

with

Not allowed.

SCRIPT1037: 'with' statements are not allowed in strict mode

JavaScript
with (Math){
    x = cos(3);
    y = tan(7);
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值