본문 바로가기

Web/Javascript(ES6)

[ES6] use strict

use strict

ES5에서 사용


strict 모드의 제한

1. 선언하지 않고 변수를 사용할 때.

"use strict";
x = 3.14; //선언하지 않고 사용하여 오류

2. 변수,함수,매개변수를 삭제하려할 때

"use strict";
x = 3.14;
delete x; //삭제 안됨

3. 동일한 프로퍼티를 한번 이상 선언하려 할 때

"use strict";
var x = { p1 : 10, p1 : 20}; //오류

4. 매개변수 이름이 동일할 때

"use strict";
function x(p1, p1) {}; //오류

5. 8진법의 숫자 리터럴 과 특수문자를 할당하려 할 때

"use strict";
var x = 010; //오류
var x = /010; //오류

6. 읽기전용에 할당하려 할 때

"use strict";
var obj = {};
obj.defineProperty(obj, "x", {value:0, writable:false});

obj.x = 3.14; //오류

7. 얻기 전용 (get-only)  에 할당하려 할 때

"use strict";
var obj = {get x() {return 0} };
obj.x = 3.14; //오류

8. 삭제불가능한 프로퍼티를 삭제하려 할 때

"use strict";
delete Object.prototype; //오류

9. with 키워드를 사용하려 할 때

"use strict";
with (Math){ x = cos(2) }; //오류

10. eval() 을 사용하려 할 때

"use strict";
eval("var x = 2");
alert(x); //오류


ES6에서는

use strict가 디폴트 환경

'Web > Javascript(ES6)' 카테고리의 다른 글

[ES6] Lambda  (0) 2017.03.23
[ES6] Arrow function  (0) 2017.03.23
[ES6] let, const  (0) 2017.03.23
[ES6] ECMAScript 2015  (0) 2017.03.23
지금 바로 시작하는 ES6  (0) 2017.02.17