본문 바로가기
Frontend

자바스크립트에서 'use strict' 란?

by chuckolet 2018. 12. 9.

Image result for use strict


자바스크립트로 만든 어플리케이션 파일을 하나 받았습니다. 맨 위에 'use strict'라고 적혀있는데 이게 뭐였더라...

엄격하게 문법 체크?를 해주는 옵션이라고 들었던 것 같아서 정확히 이놈이 하는 역할이 무엇이가 궁금하여 알아보았습니다.

Stack Overflow에 좋은 설명이 있길래 바로 공유!


해석 버전

1. 전역변수 비허용(var가 빠진 선언을 잡아내고 변수 이름에 오타를 찾아낸다)

2. Silent failing assignments(조용한 할당 실패)가 에러를 throw 해 줌(NaN = 5; 할당하기)

3. 지울 수 없는 Properties(속성)을 지우려고 할 때 에러를 throw 해 줌(delete Object.prototype)

4. 모든 객체 내의 속성 이름이 고유하는 것을 요구함(var x = {x1: "1", x1: "2"})

5. 함수 매개변수 이름이 고유해야함(function sum (x, x) {...})

6. 8진수 문법을 금지함(var x = 023; 어떤 개발자들이 앞의 0이 숫자 변화에 아무 영향을 주지 않는다고 착각할 수 있기 때문) 참고

7. with 키워드 금지

8.eval키워드가 새로운 변수를 도입하지 않음

9. 일반 이름 delete 금지(delete x;) 참고

10. 어떤 형태로도 eval과 arguments 라는 이름의 바인딩이나 할당 금지

11. 형식 매개변수(formal parameters)를 이용하여 arguments 객체의 속성에 별칭을 부여하지 않음

12. arguments.callee는 지원되지 않음


원문

The statement "use strict"; instructs the browser to use the Strict mode, which is a reduced and safer feature set of JavaScript.

List of features (non-exhaustive)

  1. Disallows global variables. (Catches missing var declarations and typos in variable names)

  2. Silent failing assignments will throw error in strict mode (assigning NaN = 5;)

  3. Attempts to delete undeletable properties will throw (delete Object.prototype)

  4. Requires all property names in an object literal to be unique (var x = {x1: "1", x1: "2"})

  5. Function parameter names must be unique (function sum (x, x) {...})

  6. Forbids octal syntax (var x = 023; some devs assume wrongly that a preceding zero does nothing to change the number.)

  7. Forbids the with keyword

  8. eval in strict mode does not introduce new variables

  9. Forbids deleting plain names (delete x;)

  10. Forbids binding or assignment of the names eval and arguments in any form

  11. Strict mode does not alias properties of the arguments object with the formal parameters. (i.e. in function sum (a,b) { return arguments[0] + b;} This works because arguments[0] is bound to a and so on. )

  12. arguments.callee is not supported

[Ref: Strict modeMozilla Developer Network]



Reference

https://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it


제 글이 도움이 되셨다면 간단하게 '공감', '댓글' 부탁드립니다!


'Frontend' 카테고리의 다른 글

구글 개발자 도구로 XPath 확인하기  (0) 2018.12.12

댓글