javascript 严格模式

  

  一,javascript 严格模式是什么?

    javascript 严格模式是在js文件中,有用‘use strict' 表示的js 文件 或者 代码块

<script>
    'use strict'
    var a =1
</script>

  二,为什么需要使用严格模式?

    1,消除javascript一些不合理,不严谨,和一些怪异行为

    2,消除代码运行的一些不安全行为,保证代码运行安全

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

    4,为未来新版javascritp做好铺垫

  三,进入标志

    在js文件中使用  'use strict'   ( 老版本( ie10一下 )会把它作为普通字符串处理,忽略掉 )

  四,如何使用严格模式?

    使用方式有两种:

      1,针对整个脚本文件  

        在脚本文件的首行使用‘use strict’

        

1 <script>
2     'use strict'
3     console.log('use strict')
4 </script>
5 
6 <script>
7     console.log('not use strict')
8 </script>

        这种方式在合并代码要注意    

 

 

      2, 针对单个函数 ( 不常用 )

 

function func(){
  'use strict'
  console.log('this function use the strict mode')            
}

 

      3, 常用写法

      

1 (fucntion(){
2     'use strict'
3      console.log('this is a file)  
4 })()
5 
6 (fucntion(){
7     'use strict'
8      console.log('this is b file)  
9 })()

                这种写法可以安全的合并多个文件,不用担心某个文件有没有使用严格模式,他们之前是互不影响的

 

   五:

 

 

 

        

 

 

 

 

      

posted @ 2017-03-02 19:08  Glen的天空  阅读(117)  评论(0编辑  收藏  举报