JavaScript之严格模式

来源:互联网 发布:pptv聚力网络电视2017 编辑:程序博客网 时间:2024/05/23 13:37

严格模式是一种特殊的执行模式,它修复了部分语言上的不足,提供更强的错误检查,增强安全性。可以对部分函数进行执行严格模式,如:

function func(){    'use strict';}

也可以对整个js文件进行执行严格模式,如:

'use strict';function func(){}

严格模式下,有几种限制:
1.不允许使用with

function func(obj){    with(obj){        console.log(x);    }}func({x:1});//1function func(obj){    'use strict';    with(obj){        console.log(x);    }}func({x:1});//SyntaxError

2、不允许未声明的变量被赋值

function func(){    x=2;    console.log(window.x);}func();//2function func(){    'use strict';    x=2;    console.log(window.x);}func();//ReferenceError

3、arguments变为参数的静态副本

function func(x){    arguments[0]=2;    console.log(x);}func(5);//2function func(x){    'use strict';    arguments[0]=2;    console.log(x);}func(5);//5

4、delete参数、函数名报错

function func(x){    console.log(delete x);}func(5);//falsefunction func(x){     'use strict';    console.log(delete x);}func(5);//SyntaxError

5、delete不可配置的属性报错

function func(){    var obj={};    Object.defineProperty(obj,'age',{configurable:false});    console.log(delete obj.age);}func();//falsefunction func(){    'use strict';    var obj={};    Object.defineProperty(obj,'age',{configurable:false});    console.log(delete obj.age);}func();//TypeError

6、对象字面量重复属性名报错

!function(){    'use strict';    var obj={x:1,x:2};//IE下会报错,chrome下不会报错,以属性的最后一个为准}();

!function(){}():
解释器在解释一个语句时,如果以function开头,就会理解为函数声明。
而前面加一个!可以让解释器理解为函数表达式,这样就可以立即调用了。
7、禁止八进制字面量

function func(){    console.log(0123);}func();//83function func(){     'use strict';    console.log(0123);}func();//SyntaxError

8、eval,arguments变为关键字,不能作为变量、函数名

function func(){    function eval(){}}func();//没有报错function func(){    'use strict';    function eval(){}}func();//SyntaxError

9、eval独立作用域

function func(){    eval('var x=1;');    console.log(x);}func();//1function func(){    'use strict';    eval('var x=1;');    console.log(x);}func();//ReferenceError

10、修改对象的不可写属性报错

function func(){    var obj={};    Object.defineProperty(obj,'a',{value:1,writable:false});    obj.a=3;    console.log(obj.a);}func();//1,忽略对属性的修改function func(){    'use strict';    var obj={};    Object.defineProperty(obj,'a',{value:1,writable:false});    obj.a=3;    console.log(obj.a);}func();//TypeError

11、为不可扩展的对象增添属性报错

function func(){    var obj={};    Object.preventExtensions(obj);    obj.a=3;    console.log(obj.a);}func();//undefined,忽略增添属性function func(){    'use strict';    var obj={};    Object.preventExtensions(obj);    obj.a=3;    console.log(obj.a);}func();//报错,TypeError

试图修改不可写属性(writable=false),在不可扩展的对象上添加属性时报TypeError,而不是忽略。
12、caller、callee禁用

function func(){    console.log(func.caller);}func();//nullfunction func(){        'use strict';        console.log(func.caller);    }func();//TypeErrorfunction func(){    console.log(arguments.callee);}func();//function func(){console.log(arguments.callee;}function func(){    'use strict';    console.log(arguments.callee);}func();//TypeError

13、call(null)、call(undefined)、apply(null)、apply(undefined),this指向null或undefined

function func(){    'use strict';    console.log(this);}func.call(null);//nullfunc.call(undefined);//undefinedfunc.apply(null);//nullfunc.apply(undefined);//undefinedfunction func(){    console.log(this);}func.call(null);//windowfunc.call(undefined);//windowfunc.apply(null);//windowfunc.apply(undefined);//windowfunction func(){    'use strict';    console.log(this);}func();//undefinedfunction func(){    console.log(this);}func();//window

严格模式下,一般函数调用时(不是对象的方法调用,也不使用apply/call/bind等修改this)this指向undefined,而不是全局对象。
若使用apply/call,当传入null或undefined时,this将指向null或undefined,而不是全局对象。

1 0