- Notifications
You must be signed in to change notification settings - Fork846
eval 번역 (원문 저장소 변경 반영)#281
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,25 +1,25 @@ | ||||||
# Eval:문자열 코드 실행하기 | ||||||
내장 함수 `eval`을 사용하면 문자열 형태의 코드를 실행할 수 있습니다. | ||||||
문법은 다음과 같습니다. | ||||||
```js | ||||||
let result = eval(code); | ||||||
``` | ||||||
예시: | ||||||
```js run | ||||||
let code = 'alert("Hello")'; | ||||||
eval(code); // Hello | ||||||
``` | ||||||
문자열 코드는 길거나 줄 바꿈, 함수 선언, 변수 등을 포함할 수 있습니다. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 길이가 긴 문자열이 코드가 될 수 있는데, 여기엔 줄 바꿈, 함수 선언, 변수 등이 포함될 수도 있습니다. | ||||||
`eval`은 마지막 구문의 결과를 반환합니다. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 마지막 구문의 결과가 | ||||||
예시: | ||||||
```js run | ||||||
let value = eval('1+1'); | ||||||
alert(value); // 2 | ||||||
@@ -30,7 +30,7 @@ let value = eval('let i = 0; ++i'); | ||||||
alert(value); // 1 | ||||||
``` | ||||||
eval로 둘러싼 코드는 현재 렉시컬 환경에서 실행되므로 외부 변수를 읽을 수 있습니다. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. eval로 둘러싼 코드는 현재 렉시컬 환경에서 실행되므로 외부 변수에 접근할 수 있습니다. | ||||||
```js run no-beautify | ||||||
let a = 1; | ||||||
@@ -46,69 +46,69 @@ function f() { | ||||||
f(); | ||||||
``` | ||||||
외부 변수 변경도 가능합니다. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 외부 변수를 변경하는 것도 가능하죠. | ||||||
```js untrusted refresh run | ||||||
let x = 5; | ||||||
eval("x = 10"); | ||||||
alert(x); // 10,변경된 값 | ||||||
``` | ||||||
엄격 모드에서`eval`은 자체 렉시컬 환경을 갖고 있습니다. 따라서 eval 내부에 선언된 함수와 변수는 외부에서 읽을 수 없습니다. | ||||||
```js untrusted refresh run | ||||||
//참고: 'use strict'는 실행 예제에서 기본적으로 활성화 되어 있음 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. // 참고: 실행 가능한 모든 예시에 'use strict'가 적용되어있습니다. | ||||||
eval("let x = 5; function f() {}"); | ||||||
alert(typeof x); // undefined (없는 변수) | ||||||
//함수 f도 읽을 수 없음 | ||||||
``` | ||||||
`use strict`을 사용하지 않으면`eval`은 자체 렉시컬 환경을 갖지 않으므로 외부에 있는`x`와`f`를 읽을 수 있습니다. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
| ||||||
## "eval" 사용하기 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 'eval' 사용하기 | ||||||
모던 프로그래밍에서`eval`은 거의 사용되지 않습니다. 흔히"eval is evil"라고 합니다. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
| ||||||
이유는 간단합니다. 오래전, 자바스크립트는 빈약한 언어였기 때문에 `eval`을 사용해야만 처리할 수 있는 것들이 많았습니다. 하지만 그 이후로 10여 년이 흐르면서 자바스크립트는 강력한 언어로 변모하였죠. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 이유는 간단합니다. 과거엔 자바스크립트에서 쓸 수 있는 기능이 많지 않았기 때문에 | ||||||
지금은`eval`을 사용할 이유가 거의 없습니다. 누군가가 여전히 eval을 사용하고 있다면, 모던한 언어 문법이나 [모듈](info:modules)을 사용해 코드를 바꾸는 걸 권유해 보시기 바랍니다. | ||||||
eval을 사용할 땐 외부 변수에 접근 시 부작용이 발생한다는 점에 유의하셔야 합니다. | ||||||
애플리케이션이 출시 되기 전에 자바스크립트 파일을 압축해주는 도구인 코드 압축기를 사용하면, 코드를 간단히 만들기 위해`a`나 `b`처럼 지역 변수명을 더 짧게 바꿀 수 있습니다. 대게 이런 최적화 과정은 안전하게 진행 되지만`eval`을 사용하면 eval로 감싼 코드가 지역 변수에 접근할 수 있으므로 안전하지 않습니다. 따라서 압축기는 `eval`에서 잠재적으로 나타나는 모든 변수의 이름을 바꾸지 않고, 이는 코드 압축률에 부정적인 영향을 미칩니다. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 애플리케이션이 출시되기 전에 자바스크립트 파일을 압축해주는 도구인 코드 압축기(minifier)는 스크립트 크기를 줄이기 위해 지역 변수명을 | ||||||
또한`eval`내부에서 외부 지역 변수를 사용하면 코드 유지 보수가 더 어려워지기 때문에 이런 방법은 좋지 않은 프로그래밍 관습으로 취급됩니다. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
| ||||||
위와 같은 문제를 완전히 예방할 방법은 두 가지가 있습니다. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 위와 같은 문제는 아래에서 소개해 드릴 방법 두 개를 사용해 예방할 수 있습니다. | ||||||
**eval로 감싼 코드를 외부 변수를 쓰지 않는다면`eval`대신 `window.eval(...)`을 호출하세요.** | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||
이렇게 하면 eval 내의 코드가 전역 스코프에서 실행됩니다. | ||||||
```js untrusted refresh run | ||||||
let x = 1; | ||||||
{ | ||||||
let x = 5; | ||||||
window.eval('alert(x)'); // 1 (전역 변수) | ||||||
} | ||||||
``` | ||||||
**eval로 감싼 코드가 지역 변수를 사용한다면`eval`을`new Function`으로 변경하고 eval로 감싼 코드는 인수로 전달하세요.** | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||
```js run | ||||||
let f = new Function('a', 'alert(a)'); | ||||||
f(5); // 5 | ||||||
``` | ||||||
`new Function`문법은 챕터<info:new-function>에서 살펴봤는데, 이 문법은 인수로 받은 문자열을 기반으로 전역 스코프에 새로운 함수를 만들어줍니다. 따라서 `new Function`으로 만든 함수는 지역 변수에 접근할 수 없습니다. 하지만 위 예시처럼 `new Function`의 인수에 지역 변수를 명시해주면 훨씬 더 명확한 코드를 작성할 수 있습니다. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
| ||||||
##요약 | ||||||
`eval(code)`을 호출하면 문자열 형태의 `code`가 실행되는데 이때 마지막 구문의 결과가 반환됩니다. | ||||||
-모던 자바스크립트엔 eval을 대체할 수 있는 문법이 많기 때문에, 모던 자바스크립트를 사용하는 코드에선 eval을 잘 사용하지 않습니다. | ||||||
-eval을 이용해 만든 코드는 외부 지역 변수에 접근할 수 있는데, 이는 좋지 않은 방법입니다. | ||||||
-전역 스코프에서 사용된`eval`은`window.eval(code)`을 이용해 대체할 수 있습니다. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 전역 스코프에서 | ||||||
-외부 스코프에 있는 데이터(변수)가 필요한 코드를 작성 중이라면`new Function`에 해당 코드를 인수로 전달해 사용할 수도 있습니다. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 외부 스코프에 있는 데이터가 필요하다면 |