JS严格模式

JS严格模式
简介
“use strict” 指令在 JavaScript 1.8.5 (ECMAScript5) 中新增。
它不是一条语句,但是是一个字面量表达式,在 JavaScript 旧版本中会被忽略。
“use strict” 的目的是指定代码在严格条件下执行。
严格模式下你不能使用未声明的变量。
为了向将来Javascript的新版本过渡,严格模式新增了一些保留关键字:
implements
interface
let
package
private
protected
public
static
yield
进入严格模式之后,需要进行哪些行为变更:
1.全局变量声明时,必须加关键字(var)
正常模式:
a = 10; console.log(a) //10
严格模式:a = 10; console.log(a) //a is not defined

2.this无法指向全局对象
    正常模式:function fn(){ console.log(this) }        //window
    严格模式:function fn(){ console.log(this) }        //undefined

function f(){
return !this;
}
// 返回false,因为"this"指向全局对象,"!this"就是false

function f(){
“use strict”;
return !this;
}
// 返回true,因为严格模式下,this的值为undefined,所以"!this"为true。

因此,使用构造函数时,如果忘了加new,this不再指向全局对象,而是报错。
function f(){
“use strict”;
this.a = 1;
};
f();// 报错,this未定义

3.函数内不允许出现重名参数
    正常模式:function fn( a,b,b ){ console.log(a,b) }
            fn(1,2,3)        //1,3
    严格模式:function fn( a,b,b ){ }
     //报错:Duplicate parameter name not allowed in this context    在此上下文中不允许出现重复的参数名

4.arguments对象
    4.1 arguments对象不允许被动态改变
        正常模式:function fn(a){
                    a=20;
                    console.log(a);                //20
                    console.log(arguments[0]);     //20
                }
                fn(10);

        严格模式:function fn(a){
                    a=20;
                    console.log(a);                //20
                    console.log(arguments[0]);     //10
                }
                fn(10);
    4.2 arguments对象不允许被自调用(递归)
        正常模式:function fn(a){
                    if( a == 1 ){
                        return 1;
                    }
                    return arguments.callee(a-1) + a;
                }
                fn(3);            //6
        严格模式:function fn(a){
                    if( a == 1 ){
                        return 1;
                    }
                    return arguments.callee(a-1) + a;
                }
                //报错:'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
                //报错:"caller","arguments","callee",不能在严格模式下使用

严格模式的限制
不允许删除变量或对象。

“use strict”;
var x = 3.14;
delete x; // 报错

不允许删除函数。

“use strict”;
function x(p1, p2) {};
delete x; // 报错

不允许使用八进制:

“use strict”;
var x = 010; // 报错

不允许使用转义字符:

“use strict”;
var x = \010; // 报错

不允许对只读属性赋值:

“use strict”;
var obj = {};
Object.defineProperty(obj, “x”, {value:0, writable:false});

obj.x = 3.14; // 报错

不允许对一个使用getter方法读取的属性进行赋值

“use strict”;
var obj = {get x() {return 0} };

obj.x = 3.14; // 报错

不允许删除一个不允许删除的属性:
“use strict”;
delete Object.prototype; // 报错

变量名不能使用 “eval” 字符串:

“use strict”;
var eval = 3.14; // 报错

变量名不能使用 “arguments” 字符串:

“use strict”;
var arguments = 3.14; // 报错

不允许使用以下这种语句:

“use strict”;
with (Math){x = cos(2)}; // 报错

由于一些安全原因,在作用域 eval() 创建的变量不能被调用:

“use strict”;
eval (“var x = 2”);
alert (x); // 报错

参考文章

https://www.cnblogs.com/FD-1909/p/11561392.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值