什么是 “use strict”? 使用它的好處和壞處是什么?


嚴格模式是ES5引入的,更好的將錯誤檢測引入代碼的方法。顧名思義,使得JS在更嚴格的條件下運行。

變量必須先聲明,再使用
function test(){
  "use strict";
  foo = 'bar';  // Error
}
 
不能對變量執行delete操作
var foo = "test";
function test(){}
 
delete foo; // Error
delete test; // Error
 
function test2(arg) {
    delete arg; // Error
}
對象的屬性名不能重復
{ foo: true, foo: false } // Error
 
禁用eval()
 
函數的arguments參數
setTimeout(function later(){
  // do stuff...
  setTimeout( later, 1000 );
}, 1000 );
 
禁用with(){}
 
不能修改arguments
不能在函數內定義arguments變量
不能使用arugment.caller和argument.callee。因此如果你要引用匿名函數,需要對匿名函數命名。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM