2013-11-03 50 views
3

我有一個JavaScript應用程序,我想知道是否通過刪除所有「嚴格使用」在程序中的聲明,我會以某種方式改變它的行爲。
據我所知,嚴格模式不允許一些東西,一旦應用程序完成開發,我可以刪除它,而不會造成任何副作用。
也提到了'this'這個變量,提到here,但Chrome似乎並沒有將此行爲實施到現在。
謝謝!嚴格模式可以被刪除而沒有副作用

+1

您可能要添加到您的相關頁面的列表:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode –

+0

有看這篇文章希望這有助於http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/ –

回答

2

在有些情況下你的代碼可能會受到影響,但大部分都是虛構的一些情況:

  • 當傳遞nullundefined作爲this值(在非嚴格模式,這是轉換成全局對象,而不是一個空的對象):

    'use strict'; 
    (function() { 
        if (!this) console.log('performs proper actions'); 
        else console.log('fail! oops...'); 
    }).call(undefined); // 'performs proper actions' 
    

    在嚴格模式下,這將登錄"performs proper actions"。然而,這是不是在非嚴格模式,在那裏你會得到失敗的消息相同:

    (function() { 
        if (!this) console.log('performs proper actions'); 
        else console.log('fail! oops...'); 
    }).call(undefined); // 'fail! oops...' 
    

    它也有同樣的行爲,如果你使用null代替undefined

  • 如果您的功能依賴於this值未被強制轉換爲對象 - 在非嚴格模式下,this被隱式強制轉換爲對象。例如,值如false'Hello World!'NaNInfinity1將被強制爲他們的對象包裝當量(如前面提到的,nullundefined有自己的行爲)。比較嚴格模式會發生什麼:

    'use strict'; 
    (function() { console.log(this); }).call(1); // 1 
    

    ...在非嚴格模式下會發生什麼:

    (function() { console.log(this); }).call(1); // '[object Number]' 
    
  • 當依靠正式的參數和arguments對象上分配不分享他們的價值觀:

    function strict(a, b, c) { 
        'use strict'; 
        var a = 1; 
        var b = 2; 
        var c = 3; 
        var d = 4; 
        console.log('strict: ' + (arguments[0] === a ? 'same' : 'different')); // different 
        console.log('strict: ' + (arguments[1] === b ? 'same' : 'different')); // different 
        console.log('strict: ' + (arguments[2] === c ? 'same' : 'different')); // different 
        console.log('strict: ' + (arguments[3] === d ? 'same' : 'different')); // of course they're different; by design 
    } 
    
    function notStrict(a, b, c) { 
        var a = 1; 
        var b = 2; 
        var c = 3; 
        var d = 4; 
        console.log('non-strict: ' + (arguments[0] === a ? 'same' : 'different')); // same 
        console.log('non-strict: ' + (arguments[1] === b ? 'same' : 'different')); // same 
        console.log('non-strict: ' + (arguments[2] === c ? 'same' : 'different')); // same 
        console.log('non-strict: ' + (arguments[3] === d ? 'same' : 'different')); // of course they're different; by design 
    } 
    
    strict(0, 1, 2, 3); 
    notStrict(0, 1, 2, 3); 
    

    你得到的是如下:

    strict: different 
    strict: different 
    strict: different 
    strict: different 
    non-strict: same 
    non-strict: same 
    non-strict: same 
    non-strict: different 
    
  • 如果您一直在使用eval並直接調用它,您會發現eval調用中聲明的變量和函數泄漏到周圍的作用域,而不是在嚴格模式下處於其自己的作用域中。例如,a保留在嚴格模式下其原始值:

    'use strict'; 
    var a = 42; 
    eval('var a = -Infinity;'); 
    console.log(a); // 42 
    

    ...而在非嚴格模式,它被賦予了新的價值:

    var a = 42; 
    eval('var a = -Infinity;'); 
    console.log(a); // -Infinity 
    

    如果你是依靠新範圍被創建,這將是對你的代碼的重大改變。

  • 如果你刻意使用的方式,一個ReferenceError被拋出,如果你試圖將分配給尚未定義的變量,這會影響方式您的代碼運行:

    try { 
        randomVariableThatHasntBeenDefined = 1; 
    } catch (e) { 
        alert('performs logic'); 
    } 
    

    警報將不以非嚴格模式顯示。

所有這些都可以在ECMAScript 5.1 Specification,這是在每一種情況下會發生什麼權威參考Annex C找到。雖然閱讀起來不太容易,但瞭解特殊的角落案例以及他們爲什麼會這樣做會很有用。

1

在大多數情況下,嚴格模式僅限制代碼的功能,但不能認爲移除嚴格模式不會改變行爲。例如,在嚴格模式和正常模式下,使用arguments陣列是不同的。

例子:

function test(a) { 
    "strict mode"; 
    a = 42; 
    return arguments[0]; 
} 

如果調用此函數test (1337)它會返回1337,但如果你從中取出嚴格模式,它反而會返回42

有關的完整列表什麼嚴格模式的作用:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode