zoukankan      html  css  js  c++  java
  • 严格模式

    严格模式又叫use strict开启分成两种 :  1. 全局开启 ; 2. 局部开启

    01. 严格模式的目的

    1)消除Javascript语法的一些不合理、不严谨之处,减少一些怪异行为;

    2)消除代码运行的一些不安全之处,保证代码运行的安全;

    3)提高编译器效率,增加运行速度;

    4)为未来新版本的Javascript做好铺垫。

    02. 严格模式的变更

    1 开启严格模式

    想开启严格模式,直接在作用域开始的位置写上 字符串 use strict

    2)严格模式的规则

    关键字:var with this 、参数、八进制、arguments

    03.区别 :

            1. var 使用的区别 ;  消除了伪全局变量!;

            (function(){

                "use strict";

                a = 10;

            })();

            (function(){

                a = 10;

                console.log(a);

            })();

            2. 对函数参数的要求更严格了;

            

            (function(){

                function foo(a,a,b){

                    console.log(a,b);

                 }

                foo(1,2,3)

            })();

            

            (function(){

                "use strict";

                function foo(a,a,b){

                    console.log(a,b);

                }

                foo(1,2,3)

            })();

            

            3. 实参形参 和 arguments的分离;

            arguments 会受到 形参赋值的影响;

            (function(){

                function foo(a,b){

                    a = 20;

                    console.log(a,b);

                    console.log(arguments[0],arguments[1]);

                }

                foo(1,2)

            })();

            

            (function(){

                "use strict";

                // 形参 和 arguments 之间的区别;

                // 形参是变量可以随意赋值;

                // arguments 就是对应实参的关键字获取所有的实参,进行使用,不会被改变;

                function foo(a,b){

                    a = 20;

                    console.log(a,b);

                    console.log(arguments[0],arguments[1]);

                }

                foo(1,2)

            })();

            4. arguments 的严格使用; 部分功能禁用了;

            (function(){

                function foo(){

                   // arguments.callee 指向了当前的函数;

                   console.log(arguments.callee);

                }

                foo()

            })();

            

            (function(){

                "use strict";

                function foo(){

                   // 禁用掉了大部分arguments的属性;

                   console.log(arguments.callee)

                }

                foo()

            })();

    5. 严格模式之中对this的严格;

            在以后的编程之中,this最没有用的指向就是window;

            (function(){

                function foo(){

                    this 指向window;

                   console.log(this);

                }

                foo()

            })();

    (function(){

                "use strict";

                function foo(){

                    // 禁用指向了windowthis,让this指向undefined

                   console.log(this);

                }

                foo()

            })();

            6. with(){}

            使用with语句语法会混乱;

            使用with会很大程度上降低代码的性能;

    (function(){    

                with(Math){

                    // 可以省略对象前置;

                    console.log(random());

                    console.log(PI);

                }

            })();

             严格模式之下禁用with(){}

            (function(){

                "use strict";

                with(Math){

                    console.log(random());

                    console.log(PI);

                }

            })();

            7. 在严格模式之中被禁用的进制,不允许使用八进制;

            (function(){

                // 0 开头就是八进制的标志;

                console.log(012);

            })();

            (function(){

                "use strict"

                console.log(012)

            })();

    04.总结 :

            1. var : 不允许声明伪全局变量;

            2. 参数: 不允许重复的参数;

            3. arguments 和参数的关系; 进行了分离;

               arguments 禁用了 callee 属性;

            4. this指向了window, 会让this的指向抛空;

            5. 禁用了with语句;

            6. 严格模式之中不允许使用八进制;

  • 相关阅读:
    hibernate对应的annocation版本
    Struts 2 OGNL
    Struts2的Stack Context和ValueStack
    Struts2中的OGNL详解
    struts2中根对象以及ognl .
    在Struts 2中实现IoC
    Struts2的属性驱动与模型驱动的区别
    Struts2的模型驱动
    Java中线程的锁和数据库中的事务隔离级别
    为什么socket编程要用到多线程
  • 原文地址:https://www.cnblogs.com/wyy0217/p/12502824.html
Copyright © 2011-2022 走看看