JS strict 模式

來源:互聯網
上載者:User

標籤:

use strict是ECMAscript5的特性,不建議在全域使用use strict,因為當壓縮不同檔案時候,一個檔案使用了use strict,一個沒有使用,沒有使用use strict的檔案就會報錯

1.作用

  消除js文法不合理、不嚴謹、不安全的問題;提高編譯器效率,增加運行速度

2.支援的瀏覽器

  IE10+,Firefox4+,safari12+,opera12+,chrome

3.規則

  變數:

    不允許建立全域變數 eg: name = "bella"; 

    不能對變數進行delete操作 eg: var name = "bella"; delete name;

  對象:

    不能為唯讀屬性賦值:

      ECMAscript5出現了Object.definePropotype方法改變了javascript語言中所有object是Public的這一說法;

      Object.definePropotype(obj,prop,descriptor):需要添加或修改的對象,屬性名稱,屬性描述;

      var person = {name:"bella",sex:"female"};Object.definePropotype(person ,"name",{value:"alias",writable:false})

    不能為不可配置的屬性使用delete操作

      不可配置也是通過Object.definePropotype來設定的

      var person = {name:"bella",sex:"female"};Object.definePropotype(person ,"name",{configurable:false})

  函數:

    參數必須唯一 eg: function(a,a){}

    修改形參不會影響arguments  eg:funtion(arg1){ arg1 = "bella";} 非strict 模式下arguments[0]:bella,arguments被修改了

    不允許使用arguments.callee 和 arguments.caller

  不允許eval()在包含上下文中建立變數或函數

    一般都很少用eval,因為eval會將字串當做js代碼來執行,會改變上下文 eg:eval("var x = 10"); alert(x)這樣是不允許的

  不允許this為null 或 undefined

  不允許使用with語句

  不允許使用八進位字面量

 

JS strict 模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.