2013-11-03 4 views
3

JavaScript 응용 프로그램이 있는데 "strict strict"를 모두 제거하면됩니다. 프로그램의 문장, 나는 어떤면에서 그 행동을 변화시킬 것이다.
엄격한 모드는 일부 항목을 허용하지 않으며 응용 프로그램의 개발이 끝나면 부작용없이 제거 할 수 있습니다.
here이라고 표시된 'this'변수의 경우도 있지만 Chrome에서 지금까지이 동작을 구현하지 않은 것처럼 보입니다.
감사합니다.strict 모드를 부작용없이 제거 할 수 있습니까?

+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

그들 중 대부분은 오히려 인위적인 비록 코드가 영향을받을 수있는 몇 가지 경우가 있습니다

이 비 엄격 모드에서합니다 ( this 값으로 null 또는 undefined 통과가 변환됩니다
  • 전역 객체가 아닌 빈 개체) : 엄격 모드에서

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

    ,이 "performs proper actions"를 기록 할 것입니다. 대신 undefinednull를 사용하는 경우 또한 같은 동작을 가지고

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

    그러나, 이것은 당신이 실패 메시지가 줄 비 엄격 모드에서 동일하지 않습니다.

  • 함수가 this 값이 객체에 강제되지 않는 것에 의존하는 경우 - 비 엄격 모드에서 this은 객체에 대해 암시 적으로 강제됩니다. 예를 들어, false, 'Hello World!', NaN, Infinity1과 같은 값은 해당 객체 래퍼에 해당하는 것으로 강제 변환됩니다 (앞에서 설명한 것처럼 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 SpecificationAnnex 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

관련 문제