以下是嚴格模式中需要注意的用法,這里需要強調(diào)的是:ES6 的 class 和 模塊內(nèi)都是默認的嚴格模式。其實,js 開發(fā)也會逐步走向嚴格模式,這應(yīng)該是個趨勢。
s添加了保留字 protected,static 和 interface
在嚴格模式下,不可以用with()
(function(){
//非嚴格模式
var a = {name: "Bob"};
with(a){
name = "Lily";
}
console.log(a.name); //Lily
})();
(function(){
"use strict"; //嚴格模式
var a = {name: "Bob"}, b = {};
with(a, b){ //SyntaxError: Strict mode code may not include a with statement
name = "Lily";
}
console.log(a.name);
})();
在嚴格模式下,變量必須顯示聲明(var/let/const)
(function(){
//非嚴格模式
a = 10;
console.log(a); //10
})();
(function(){
"use strict"; //嚴格模式
b = 10; //ReferenceError: b is not defined
console.log(b);
})();
在嚴格模式下,this默認是undefined
(function(){
//非嚴格模式
console.log(this); //window
})();
(function(){
"use strict"; //嚴格模式
console.log(this); //undefined
})();
在嚴格模式下,為只讀變量和不可擴展對象賦值會報錯, 而不是靜默失敗
(function(){
//非嚴格模式
var o = {
name: "Lily"
};
Object.freeze(o);
o.name = "Bob";
o.age = 20;
console.log(o); //Object {name: "Bob"}
})();
(function(){
"use strict"; //嚴格模式
var o = {
name: "Lily"
};
Object.freeze(o);
o.name = "Bob"; //TypeError: Cannot assign to read only property 'name' of object '#<Object>'
o.age = 20; //TypeError: Can't add property age, object is not extensible
console.log(o);
})();
在嚴格模式下,不可以在eval參數(shù)中定義變量和函數(shù)
(function(){
//非嚴格模式
var str1 = "var name='Lily';";
var str2 = "function fun1(){console.log('hello');}";
eval(str1); //這個name定義在了全局,而不是函數(shù)內(nèi)
eval(str2);
console.log(name); //Lily
fun1(); //hello
})();
(function(){
"use strict"; //嚴格模式
var str1 = "var alias='Lily';";
var str2 = "function fun2(){console.log('hello');}";
eval(str1);
eval(str2);
eval("name = 'Bob'"); //修改全局變量name
console.log(name); //Bob
console.log(alias); //ReferenceError: alias is not defined
fun2(); //ReferenceError: fun is not defined
})();
在嚴格模式下,有名參數(shù)是arguments參數(shù)的靜態(tài)副本,而非引用。
(function(){
//非嚴格模式
var name = "Bob";
test(name);
function test(alias){
alias = "Lily";
console.log(alias); //Lily
console.log(arguments[0]); //Lily
}
})();
(function(){
"use strict"; //嚴格模式
var name = "Bob";
test(name);
function test(alias){
alias = "Lily";
console.log(alias); //Lily
console.log(arguments[0]); //Bob
}
})();
在嚴格模式下,用delete刪除var聲明的變量和不可配置屬性時拋出異常,而不是靜默失敗(返回false)
(function(){
//非嚴格模式
var a = 10;
var fun = function(){console.log("fun called");};
var o = Object.defineProperty({}, "name", {
value: "Bob"
}); //默認即不可配置
delete a; //false
console.log(a); //10
delete fun; //false
fun(); //fun called
delete o.name; //false
console.log(o.name); //Bob
//刪除一個不存在的變量
delete no; //false
})();
(function(){
"use strict"; //嚴格模式
var a = 10;
var fun = function(){console.log("fun called");};
var o = Object.defineProperty({}, "name", {
value: "Bob"
}); //默認即不可配置
//delete a; //SyntaxError: Delete of an unqualified identifier in strict mode.
console.log(a);
delete fun; //SyntaxError: Delete of an unqualified identifier in strict mode.
fun();
delete o.name; //SyntaxError: Delete of an unqualified identifier in strict mode.
console.log(o.name);
//刪除一個不存在的變量
delete no; //SyntaxError: Delete of an unqualified identifier in strict mode.
})();
在嚴格模式下,arguments和eval是關(guān)鍵字,不能被修改
(function(){
//非嚴格模式
eval = 10;
eval("console.log('hello');"); //TypeError: eval is not a function
(function(){
arguments = 20;
console.log(arguments); //20
}());
})();
(function(){
"use strict"; //嚴格模式
eval = 10; //SyntaxError: Unexpected eval or arguments in strict mode
eval("console.log('hello');");
(function(){
arguments =20; //SyntaxError: Unexpected eval or arguments in strict mode
console.log(arguments);
}());
})();
在嚴格模式下,不可以用8進制
(function(){
//非嚴格模式
console.log(070); //56 (因瀏覽器而異)
})();
(function(){
"use strict"; //嚴格模式
console.log(070); //SyntaxError: Octal literals are not allowed in strict mode.
})();
在嚴格模式下,函數(shù)的形參不可以同名
(function(){
//非嚴格模式
var one = 1;
var two = 2;
fun(one, two); //2
function fun(a,a){
console.log(a);
}
})();
(function(){
"use strict"; //嚴格模式
var one = 1;
var two = 2;
fun(one, two);
function fun(a,a){ //SyntaxError: Duplicate parameter name not allowed in this context
console.log(a);
}
})();
在嚴格模式下,不可以使用caller和arguments的屬性,會報錯
(function(){
//非嚴格模式
A();
function A(){
B();
}
function B(){
console.log(B.caller); //function A(){ B(); }
console.log(arguments.callee); //function B(){ console.log(B.caller); console.log(arguments.callee); }
}
})();
(function(){
"use strict"; //嚴格模式
A();
function A(){
B();
}
function B(){
console.log(B.caller); //TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.
console.log(arguments.callee); //TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
}