Use strict (엄격 모드)
Use Strict 사용 이유
-
javascript환경에서 실수를 에러로 바꿔놓는다.
-
javascript 올바르지 않은 문법들을 사전에 검출할 수 있다.
(쓰기금지 프로퍼티의 정의, getter 전용 프로터피, 존재 하지 않는 프로퍼티, 존재하지 않는 변수, 존재하지 않는 객체에 대해 에러를 발생시킵니다.)
Use Strict 적용 위치
1.코드 앞쪽 적용
"use strict"
let v = "use strict example";
2.함수 내부 적용
function strict(){
'use strict';
function online() {return "hi my name is bohun"}
}
3.strict 모드에서 허용하지 않는 문법
- 정의되지 않은 변수 사용
: x = 3 이라는 변수를 잘못 입력하면 전역 객체에 한 새 속성이 만들어 지고 그대로 동작하기 때문에 strict 모드로 오류를 발생시킨다.
'use strict'
x = 3 //index.js:3 Uncaught ReferenceError: x is not defined
- 삭제할 수 없는 프로퍼티 삭제 시도 할 시
'use strict'
x = 3
delete x //Delete of an unqualified identifier in strict mode
- 함수 파라미터 중복된 이름
'use strict'
function code(x1 ,x1){}; //Duplicate parameter name not allowed in this context
- 8진수
'use strict'
let x = 010; //Octal literals are not allowed in strict mode.
- 읽기 전용 프로퍼티에 값 설정
"use strict";
var obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false}); obj.x = 3.14;
//Cannot assign to read only property 'x' of object '#<Object>'
- 조회 전용 프로퍼티 값 설정
"use strict";
var obj = {get x() {return 0} }; obj.x = 3.14;
// TypeError: Cannot set property x of #<Object> which has only a getter
at index.js:2
- eval, arguments 문자열에 대한 변수로의 사용
"use strict";
let eval = 3.14;
let arguments = 3.14;
// Unexpected eval or arguments in strict mode
- with 사용
"use strict";
with (Math){x = cos(2)};
//Strict mode code may not include a with statement
- eval() 에 정의된 변수 사용
"use strict";
eval ("var x = 2");
alert (x);
// x is not defined
자료 출처 :
1. https://www.w3schools.com/js/js_strict.asp
2.https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Strict_mode
Author And Source
이 문제에 관하여(Use strict (엄격 모드)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@bohun-kim/Use-strict-엄격-모드저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)