“use strict”이란 커맨드가 JavaScript에서 무엇이고, 어떻게 작동되나요?

LONGNEW·2021년 1월 3일
0

StackOverFlaw

목록 보기
10/16

Q. What does “use strict” do in JavaScript, and what is the reasoning behind it?
Q. “use strict”이란 커맨드가 JavaScript에서 무엇이고, 어떻게 작동되나요?

Recently, I ran some of my JavaScript code through Crockford's JSLint, and it gave the following error:
최근, 저는 JavaScript로 코드를 작성하고 JSLint로 실행했습니다. 그리고 아래와 같은 에러가 발생했습니다.

Problem at line 1 character 1: Missing "use strict" statement.

Doing some searching, I realized that some people add "use strict"; into their JavaScript code.
검색을 하면서, 몇몇 사람들이 "use strict";를 자신의 코드에 추가한 것을 봤습니다.
Once I added the statement, the error stopped appearing.
저 문구를 추가하니까, 에러는 사라졌습니다.
Unfortunately, Google did not reveal much of the history behind this string statement.
불행하게도, 구글은 이 문구의 기록을 찾는것에 별 도움이 되지 않았습니다.
Certainly it must have something to do with how the JavaScript is interpreted by the browser, but I have no idea what the effect would be.
확실히 JavaScript가 브라우저에서 실행될때 뭔가를 하는 거 같은데, 그게 뭔지 모르겠습니다.
So what is "use strict"; all about, what does it imply, and is it still relevant?
그래서, "use strict";이 무엇이고, 어떤 이유에서 쓰이는가요?
Do any of the current browsers respond to the "use strict"; string or is it for future use?
현재의 브라우저가 "use strict";에 반응을 하나요 아니면 미래에 이용할때 하는 건가요?


This article about Javascript Strict Mode might interest you:
아래의 Javascript Strict Mode애 관한 기사가 도움이 되겠순요.
https://johnresig.com/blog/ecmascript-5-strict-mode-json-and-more/

To quote some interesting parts:
흥미로운 부분에 대해서.

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context.
Strict Mode란 ECMAScript 5 의 새로운 기능으로 사용자가 'strict'한 실행 중에 프로그램, 함수등을 배치할 수 있게 해줍니다.
This strict context prevents certain actions from being taken and throws more exceptions.
'strict' 상황은 많은 예외적인 상황들을 예방합니다.

And:

Strict mode helps out in a couple ways:
Strict mode는 다양한 도움을 주죠.

  • It catches some common coding bloopers, throwing exceptions.
    잘못된 상황들을 잡고, 예외를 발생시킵니다.
  • It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).
    '안전하지 않은' 상황을 예방하거나 에러를 발생시킵니다
  • It disables features that are confusing or poorly thought out.
    혼란스럽게 하거나, 불필요한 특성들이 실행되지 않게 합니다.

Also note you can apply "strict mode" to the whole file... Or you can use it only for a specific function (still quoting from John Resig's article):
'strict mode'를 사용하는 방법은 두가지로.
1. 전체 파일에 이용.
2. 특정한 파일에만 이용

// Non-strict code...

(function(){
  "use strict";

  // Define your library strictly...
})();

// Non-strict code...

So, I suppose it's a bit like the "use strict" you can use in Perl (hence the name?): it helps you make fewer errors, by detecting more things that could lead to breakages.
프로그램을 파손시키는 것들을 찾아내서 에러를 적게 발생시킵니다.

Strict mode is now supported by all major browsers.
"Strict mode"는 대부분의 브라우저에서 이용이 가능합니다.
Inside native ECMAScript modules (with import and export statements) and ES6 classes, strict mode is always enabled and cannot be disabled.

0개의 댓글