严格模式->一种特殊的运行模式,修复了部分语言上的不足,增加了安全性
"use strcit";
以下俩个代码说出了"use strict"的小坑
var a = 10;
"use strict"; //由于前面有var 语句了 所以"use strict"语句失效
b = 20;
console.log(b); //20
前面只是字面量就不会失效
"a b b b b a";
"use strict";
a = 10;
console.log(a);
function foo(a){
arguments[0] = 100; //假若不传参数的话a始终为undefined
console.log(a);
}
foo(10); //100
foo(); //undefined
严格模式下不能使用with语句
严格模式下不能不用var 来声明变量
严格模式下arguments变为参数,对基本类型的参数无影响,对引用类型仍然有影响
严格模式下delete参数,函数名,(var a的)a将会报错
严格模式下delete不可配置的属性也会报错
严格模式下delete一个未定义的属性或者值报错,一般情况下返回true
严格模式下对象字面量重复属性名时会出错
严格模式下禁止八进制字面量
严格模式下禁止eval,arguments最为函数名,变量名
严格模式下arguments.callee,argumenst.caller,caller属性都不可以使用
function foo(a){
arguments[0] = 100;
console.log(a);
}
foo(10); //100
function foo(a){
"use strict";
arguments[0] = 100;
console.log(a);
}
foo(10); //10
function foo(obj){
arguments[0].x = 100;
console.log(obj.x);
}
foo({x:10}); //100
function foo(obj){
"use strict";
arguments[0].x = 100;
console.log(obj.x);
}
foo({x:10}); //100
function foo(a){
console.log(a);
console.log(delete a); //false
console.log(delete foo); //false
};
foo(1);
function foo(a){
"use strict";
console.log(a);
console.log(delete a); //严格模式下报错
console.log(delete foo); //严格模式下报错
};
foo(1);
var obj = {};
Object.defineProperty(obj,"x",{
value: 1,
writable: true,
configurable: false, //可配置性
enumerable: true //可枚举性
});
for (a in obj){
console.log(a);
}
console.log(delete obj.x); //false
console.log(delete x); //delete一个为定义的属性或者值 返回true
"use strict";
var obj = {};
Object.defineProperty(obj,"x",{
value: 1,
writable: true,
configurable: false, //可配置性
enumerable: true //可枚举性
});
for (var a in obj){
console.log(a);
}
console.log(delete obj.x); //false
console.log(delete x); //delete一个为定义的属性
var obj = {
x: 1,
x: 2
}; //一般状况下这种事正常的,而且以最后一个为准
console.log(obj.x); //2
"use strict";
var obj = {
x: 1,
x: 2 //报错
};
console.log(obj.x);
console.log(0123); //8进制自动转为10进制输出
"use strict";
console.log(0123); //报错
function eval(){
console.log(1);
}
eval(); //一般模式下正常
var arguments = 10;
console.log(arguments); //一般模式下正常
"use strict";
function eval(){
console.log(1);
}
eval(); //报错
var arguments = 10;
console.log(arguments); //报错
原文地址:http://9865481.blog.51cto.com/9855481/1669511