关于javascript严格模式下七种禁止使用的写法

分享至javascript语言精髓与编程实践

开启严格模式(”use strict"):

在全局代码的开始处加入

在eval代码的开始处加入

在函数声明代码处加入

在new Function() 所传入的body参数块开始处加入

1:在对象中声明相同的属性名

   例如: var obj ={

                   'name': 1,

                   'name': 2

           };

会抛出SyntaxError: Duplicate data property in object literal not allowed in strict mode.

2:在函数声明中相同的参数名

例如: "use strict";

     function fix(a,b,a) {     

         return a+b;     

     }

会抛出 SyntaxError: Strict mode function may not have duplicate parameter names .

3:不能用前导0声明8进制直接量

例如: var a = 012;

会抛出 SyntaxError: Octal literals are not allowed in strict mode.

4: 不能重新声明、删除或重写eval和arguments这两个标示符

var eval = ......;

会抛出  SyntaxError: Assignment to eval or arguments is not allowed in strict mode

5:用delete删除显示声明的标识符、名称和具名函数

    function temp() {

       'use strict';

        var test = 1;

        delete test;

    }

会抛出 SyntaxError: Delete of an unqualified identifier in strict mode.

6.代码中使用扩展的保留字,例如 interface,let,yield,package,private等

  会抛出SyntaxError: Unexpected strict mode reserved word

7.严格模式下是禁止使用with的

会抛出 SyntaxError: Strict mode code may not include a with statement

 

 

好了以上都是摘自书上,其实我并不使用严格模式的,但是这些知识还是要储备一些。

原文地址:https://www.cnblogs.com/God-Shell/p/3139329.html