javascript中严格模式的使用

严格模式是ES5中添加的一种特殊的执行模式,提供错误检测,增强了安全性

语法:

"use strict"
  • 全局使用strict
  • 函数内使用

1: 全局变量

在strict模式下,不可以给一个为声明的变量赋值

正常模式:

<script>
    a = 1;
    console.log(a)
</script>

1

严格模式:

<script>
    "use strict"
    a = 1;
    console.log(a)
</script>

Uncaught ReferenceError: a is not defined 

2:不可写变量

正常模式:

<script>
    undefined = 1;
    console.log(undefined)
</script>

undefined

严格模式:

<script>
    "use strict"
    undefined = 1;
    console.log(undefined)
</script>

Uncaught TypeError: Cannot assign to read only property 'undefined' of object '#<Window>'(…) 

3:删除机制

在正常模式下删除不可删除的变量或属性时并不会报错(对于删除变量会返回 false ),然而在 strict 模式下删除不可删除的变量货属性时则是不可行的,因为为报错。

正常模式:

<script>
    var a = 1;
    delete a;
</script>

true

严格模式:

<script>
    "use strict"
    var a = 1;
    delete a
</script>

Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.(…)

4: 命名的唯一性

对于 strict 模式下,函数的参数和对象内的变量都不能有重命名现象。
而在正常模式下是允许的。

正常模式:

<script>
    function test(a,a){
        return a * a;
    }
</script>

严格模式:

<script>
    "use strict"
    function test(a,a){
        return a * a;
    }
</script>

Uncaught SyntaxError: Duplicate parameter name not allowed in this context(…)

5:八进制的支持

strict 模式不支持数字的八进制表示,我们知道在正常模式下,第一位为0的数字将会认为是八进制,但是在 strict 模式不允许。

正常模式:

<script>
    var a = 01;
</script>

严格模式:

<script>
    "use strict";
    var a = 01;
</script>

Uncaught SyntaxError: Octal literals are not allowed in strict mode.(…)

6:不允许使用with

Javascript 的 strict 模式很重要的一个改变就是变量在编译阶段就已经确定,
with 语句在 strict 模式下是不被支持的,因为我们都知道 with 语句只有在运行时才能确定变量。

正常模式:

!function(){
    with({x:1}){
        console.log(x)
    }
}();

严格模式:

!function(){
    'use strict';
    with({x:1}){
        console.log(x)
    }
}();

Uncaught SyntaxError: Strict mode code may not include a with statement(…)

7:eval 函数作用域

正常模式下, eval 函数在哪个作用域下执行就属于哪个作用域,例如在全局作用域下执行则属于全局作用域,但在 strict 模式下, eval 函数拥有自己的作用域。

正常模式:

var a = 1;
eval('var a = 2');
console.log(a);

2

严格模式

"use strict";
var a = 1;
eval('var a = 2');
console.log(a);

1

strict 模式下,不可以对 eval 赋值。

"use strict";
eval = 1;

Uncaught SyntaxError: Unexpected eval or arguments in strict mode(…)

8 :arguments 对象

在 strict 模式下对函数的额 arguments 对象做了很大的限制。
首先, arguments 跟 eval 一样,不可被赋值。

"use strict";
arguments = 1;

Uncaught SyntaxError: Unexpected eval or arguments in strict mode

strict 模式不再支持 arguments.callee 的使用。

正常模式:

!function(a){
    arguments[0] = 100;
    console.log(a)
}(1)

100

严格模式:

!function(a){
    "use strict";
    arguments[0] = 100;
    console.log(a)
}(1)

1

!function(a){
    "use strict";
    arguments[0].x = 100;
    console.log(a.x)
}({x:1})

100

9:函数体内的 this

在正常模式下,我们都知道函数体内的 this 将指向全局对象,然而在 strict 模式下, this 不仅不会指向全局对象,而且在函数定义体内直接使用还会报错,只有在使用 new 创建新对象实例时才可以使用 this ,而且指向的是新的对象实例,并非全局对象。

正常模式:

var a = 1;
function foo() {
  console.log(this.a);
}
foo();

1

严格模式:

"use strict";
 var a = 1;
function foo() {
  console.log(this.a);
}
foo();

Uncaught TypeError: Cannot read property 'a' of undefined

10:函数体内访问调用栈

在 strict 模式下,函数体内不再运行访问调用栈。

正常模式:

function foo() {
  foo.callee;
  foo.caller;
  foo.arguments;
}
foo();

严格模式:

"use strict";
function foo() {
  foo.callee;
  foo.caller;
  foo.arguments;
}
foo();

Uncaught TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.

11:函数声明的位置

在正常模式下,函数声明可以在任何位置,然后再 strict 模式下,必须在全局作用域或函数作用域的顶端声明函数。

正常模式:

var a = 1;
if(a === 1) {
  function foo() {}
}

function foo() {}

严格模式:

"use strict";
var a = 1;
if(a === 1) {
  function foo() {}
}

undefined

转载于:https://www.cnblogs.com/bijiapo/p/5460148.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值