Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict Mode #42

Closed
raycon opened this issue Dec 28, 2020 · 0 comments
Closed

Strict Mode #42

raycon opened this issue Dec 28, 2020 · 0 comments

Comments

@raycon
Copy link
Owner

raycon commented Dec 28, 2020

엄격 모드

엄격 모드는 구문과 런타임 동작을 변경한다.

실수를 에러로

실수로 글로벌 변수를 생성하는 것을 불가능하게 한다.

"use strict";
                       // 전역 변수 mistypedVariable 이 존재한다고 가정
mistypedVaraible = 17; // 변수의 오타때문에 이 라인에서 ReferenceError 를 발생시킴

예외를 발생시킨다.

"use strict";

// 쓸 수 없는 프로퍼티에 할당
var undefined = 5; // TypeError 발생
var Infinity = 5; // TypeError 발생

// 쓸 수 없는 프로퍼티에 할당
var obj1 = {};
Object.defineProperty(obj1, "x", { value: 42, writable: false });
obj1.x = 9; // TypeError 발생

// getter-only 프로퍼티에 할당
var obj2 = { get x() { return 17; } };
obj2.x = 5; // TypeError 발생

// 확장 불가 객체에 새 프로퍼티 할당
var fixed = {};
Object.preventExtensions(fixed);
fixed.newProp = "ohai"; // TypeError 발생

삭제할 수 없는 프로퍼티를 삭제하려 할 때 예외를 발생시킨다.

"use strict";
delete Object.prototype; // TypeError 발생

중복된 파라미터 이름은 구문 에러를 발생시킨다.

function sum(a, a, c){ // !!! 구문 에러
  "use strict";
  return a + b + c; // 코드가 실행되면 잘못된 것임
}

0을 붙여서 사용하는 8진법 숫자 리터럴을 금지한다.

"use strict";
var sum = 015 + // !!! 구문 에러
          197 +
          142;

원시값에 프로퍼티를 설정하는것을 금지한다.

(function() {
"use strict";

false.true = "";         // TypeError
(14).sailing = "home";   // TypeError
"with".you = "far away"; // TypeError

})();

변수 사용 단순화

  • with를 사용하지 못하게 한다.
  • eval은 새로운 변수를 주위 스코프에 추가하지 않는다.
  • delete를 사용하지 못하게 한다.

eval, arguments 단순화

  • eval, arguments는 언어 문법에 바운드되거나 할당될 수 없다. (변수, 함수, 파라미터 이름 등으로 사용될 수 없다.)
  • arguments 객체가 생성한 프로퍼티를 alias 하지 않는다. (첫번째 파라미터에 값을 할당하면 arguments[0]이 변경되던 동작이 없어진다.)
  • arguments.callee를 지원하지 않는다.

JavaScript 보안

Javascript 보안 내용 참고

예약어

미래에 사용될 수 있는 몇가지 키워드들을 예약어로 지정한다.

implements, interface, let, package, private, protected, public, static, yield
@raycon raycon added the JS label Dec 28, 2020
@raycon raycon added this to the JavaScript Concepts milestone Dec 28, 2020
@raycon raycon closed this as completed Dec 29, 2020
@raycon raycon reopened this Dec 30, 2020
@raycon raycon added the done label Dec 30, 2020
@raycon raycon closed this as completed Dec 30, 2020
@raycon raycon removed the done label Dec 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant