-
Notifications
You must be signed in to change notification settings - Fork 856
[문서] C17 충돌 과제 해결 #1863
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
base: 2026-en-merge
Are you sure you want to change the base?
[문서] C17 충돌 과제 해결 #1863
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,7 @@ | |
| ```smart header="오래된 스크립트를 읽는 데 도움을 주는 글입니다." | ||
| 이번 주제에선 작성된 지 오래된 스크립트를 읽는 데 도움을 줄 만한 내용을 다룹니다. | ||
|
|
||
| <<<<<<< HEAD | ||
| 새로운 코드를 작성할 때는 이 방법을 쓰시면 안 됩니다. | ||
| ======= | ||
| That's not how we write new code. | ||
| >>>>>>> upstream/master | ||
| ``` | ||
|
|
||
| [변수](info:variables)를 다룬 첫 번째 장에서 변수 선언 방법 세 가지를 배운 바 있습니다. | ||
|
|
@@ -32,11 +28,7 @@ alert(message); // 안녕하세요. | |
|
|
||
| ## var는 블록 스코프가 없습니다. | ||
|
|
||
| <<<<<<< HEAD | ||
| `var`로 선언한 변수의 스코프는 함수 스코프이거나 전역 스코프입니다. 블록 기준으로 스코프가 생기지 않기 때문에 블록 밖에서 접근 가능합니다. | ||
| ======= | ||
| Variables, declared with `var`, are either function-scoped or global-scoped. They are visible through blocks. | ||
| >>>>>>> upstream/master | ||
|
|
||
| 예시: | ||
|
|
||
|
|
@@ -73,12 +65,8 @@ for (var i = 0; i < 10; i++) { | |
| } | ||
|
|
||
| *!* | ||
| <<<<<<< HEAD | ||
| alert(i); // 10, 반복문이 종료되었지만 'i'는 전역 변수이므로 여전히 접근 가능합니다. | ||
| ======= | ||
| alert(i); // 10, "i" is visible after loop, it's a global variable | ||
| alert(one); // 1, "one" is visible after loop, it's a global variable | ||
| >>>>>>> upstream/master | ||
| alert(one); // 1, 반복문이 종료되었지만 'one'도 전역 변수이므로 여전히 접근 가능합니다. | ||
| */!* | ||
| ``` | ||
|
|
||
|
|
@@ -94,17 +82,10 @@ function sayHi() { | |
| } | ||
|
|
||
| sayHi(); | ||
| <<<<<<< HEAD | ||
| alert(phrase); // Error: phrase is not defined | ||
| ``` | ||
|
|
||
| 위에서 살펴본 바와 같이, `var`는 `if`, `for` 등의 코드 블록을 관통합니다. 아주 오래전의 자바스크립트에선 블록 수준 렉시컬 환경이 만들어 지지 않았기 때문입니다. `var`는 구식 자바스크립트의 잔재이죠. | ||
| ======= | ||
| alert(phrase); // ReferenceError: phrase is not defined | ||
|
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. 주석에 ReferenceError대신 Error가 사용된 것 같습니다! |
||
| ``` | ||
|
|
||
| As we can see, `var` pierces through `if`, `for` or other code blocks. That's because a long time ago in JavaScript, blocks had no Lexical Environments, and `var` is a remnant of that. | ||
| >>>>>>> upstream/master | ||
| 위에서 살펴본 바와 같이, `var`는 `if`, `for` 등의 코드 블록을 관통합니다. 아주 오래전의 자바스크립트에선 블록 수준 렉시컬 환경이 만들어 지지 않았기 때문입니다. `var`는 구식 자바스크립트의 잔재이죠. | ||
|
|
||
| ## var는 변수의 중복 선언을 허용합니다 | ||
|
|
||
|
|
@@ -224,19 +205,11 @@ sayHi(); | |
|
|
||
| 이처럼 모든 `var` 선언은 함수 시작 시 처리되기 때문에, `var`로 선언한 변수는 어디서든 참조할 수 있습니다. 하지만 변수에 무언가를 할당하기 전까진 값이 undefined이죠. | ||
|
|
||
| <<<<<<< HEAD | ||
| 바로 위의 두 예시에서 `alert`를 호출하기 전에 변수 `phrase`는 선언이 끝난 상태이기 때문에 에러 없이 얼럿 창이 뜹니다. 그러나 값이 할당되기 전이기 때문에 얼럿 창엔 `undefined`가 출력되죠. | ||
|
|
||
| ### 즉시 실행 함수 표현식 | ||
|
|
||
| 과거엔 `var`만 사용할 수 있었습니다. 그런데 `var`의 스코프는 블록 레벨 수준이 아니죠. 개발자들은 `var`도 블록 레벨 스코프를 가질 수 있게 여러가지 방안을 고려하게 됩니다. 이때 만들어진 것이 '즉시 실행 함수 표현식(immediately-invoked function expressions)'입니다. 즉시 실행 함수 표현식은 `IIFE`라고 부르기도 합니다. | ||
| ======= | ||
| In both examples above, `alert` runs without an error, because the variable `phrase` exists. But its value is not yet assigned, so it shows `undefined`. | ||
|
|
||
| ## IIFE | ||
|
|
||
| In the past, as there was only `var`, and it has no block-level visibility, programmers invented a way to emulate it. What they did was called "immediately-invoked function expressions" (abbreviated as IIFE). | ||
| >>>>>>> upstream/master | ||
|
|
||
| 즉시 실행 함수 표현식을 요즘에는 자주 쓰지 않습니다. 하지만 오래된 스크립트에서 만날 수 있기 때문에 즉시 실행 함수 표현식이 무엇인지 알아 둘 필요가 있습니다. | ||
|
|
||
|
|
@@ -252,23 +225,13 @@ IIFE는 다음과 같이 생겼습니다. | |
| })(); | ||
| ``` | ||
|
|
||
| <<<<<<< HEAD | ||
| 함수 표현식이 만들어지고 바로 호출되면서, 해당 함수가 바로 실행되었습니다. 이 함수는 자신만의 변수를 갖고있네요. | ||
|
|
||
| 즉시 실행 함수를 만들 땐, 함수 표현식을 괄호로 둘러쌓아 (function {...})와 같은 형태로 만듭니다. 이렇게 괄호로 둘러싸지 않으면 에러가 발생합니다. 자바스크립트는 'function'이라는 키워드를 만나면 함수 선언문이 시작될 것이라 예상합니다. 그런데 함수 선언문으로 함수를 만들 땐 반드시 함수의 이름이 있어야 합니다. 따라서 아래와 예시를 실행하면 에러가 발생합니다. | ||
|
|
||
| ```js run | ||
| // 함수를 선언과 동시에 실행하려고 함 | ||
| function() { // <-- Error: Function statements require a function name | ||
| ======= | ||
| Here, a Function Expression is created and immediately called. So the code executes right away and has its own private variables. | ||
|
|
||
| The Function Expression is wrapped with parenthesis `(function {...})`, because when JavaScript engine encounters `"function"` in the main code, it understands it as the start of a Function Declaration. But a Function Declaration must have a name, so this kind of code will give an error: | ||
|
|
||
| ```js run | ||
| // Tries to declare and immediately call a function | ||
| function() { // <-- SyntaxError: Function statements require a function name | ||
|
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. SyntaxError대신 Error가 사용된 것 같습니다! |
||
| >>>>>>> upstream/master | ||
|
|
||
| var message = "Hello"; | ||
|
|
||
|
|
@@ -293,21 +256,12 @@ function go() { | |
| ```js run | ||
| // IIFE를 만드는 방법 | ||
|
|
||
| <<<<<<< HEAD | ||
| (function() { | ||
| alert("함수를 괄호로 둘러싸기"); | ||
| }*!*)*/!*(); | ||
|
|
||
| (function() { | ||
| alert("전체를 괄호로 둘러싸기"); | ||
| ======= | ||
| *!*(*/!*function() { | ||
| alert("Parentheses around the function"); | ||
| }*!*)*/!*(); | ||
|
|
||
| *!*(*/!*function() { | ||
| alert("Parentheses around the whole thing"); | ||
| >>>>>>> upstream/master | ||
| }()*!*)*/!*; | ||
|
|
||
| *!*!*/!*function() { | ||
|
|
@@ -325,13 +279,8 @@ function go() { | |
|
|
||
| `var`로 선언한 변수는 `let`이나 `const`로 선언한 변수와 다른 두 가지 주요한 특성을 보입니다. | ||
|
|
||
| <<<<<<< HEAD | ||
| 1. `var`로 선언한 변수는 블록 스코프가 아닌 함수 수준 스코프를 갖습니다. | ||
| 1. `var`로 선언한 변수는 블록 스코프가 아닌 함수 수준 스코프를 갖습니다. 함수 밖에서 선언한 경우엔 전역 스코프를 갖습니다. | ||
| 2. `var` 선언은 함수가 시작되는 시점(전역 공간에선 스크립트가 시작되는 시점)에서 처리됩니다. | ||
| ======= | ||
| 1. `var` variables have no block scope, their visibility is scoped to current function, or global, if declared outside function. | ||
| 2. `var` declarations are processed at function start (script start for globals). | ||
| >>>>>>> upstream/master | ||
|
|
||
| 이 외에도 전역 객체와 관련된 특성 하나가 더 있는데, 이에 대해선 다음 챕터에서 다루도록 하겠습니다. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,11 +5,7 @@ | |
|
|
||
| 브라우저 환경에선 전역 객체를 `window`, Node.js 환경에선 `global`라고 부르는데, 각 호스트 환경마다 부르는 이름은 다릅니다. | ||
|
|
||
| <<<<<<< HEAD | ||
| 전역 객체의 이름을 `globalThis`로 표준화하자는 내용이 최근에 자바스크립트 명세에 추가되었기 때문에 모든 호스트 환경이 이를 따라야 하죠. Chromium 기반이 아닌 몇몇 브라우저는 아직 `globalThis`를 지원하진 않지만, 이에 대한 폴리필(polyfill)을 쉽게 만들 수 있습니다. | ||
| ======= | ||
| Recently, `globalThis` was added to the language, as a standardized name for a global object, that should be supported across all environments. It's supported in all major browsers. | ||
|
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. 번역이 잘못된 것 같습니다..! |
||
| >>>>>>> upstream/master | ||
| 전역 객체의 표준화된 이름으로 `globalThis`가 자바스크립트 명세에 추가되었습니다. 모든 주요 브라우저에서 지원합니다. | ||
|
|
||
| 본 튜토리얼은 브라우저 환경에서 구동되기 때문에 `window`라는 전역 객체를 사용하도록 하겠습니다. 다른 호스트 환경에서 작업하고 계신다면 `window`대신 `globalThis`를 사용하시면 됩니다. | ||
|
|
||
|
|
@@ -29,13 +25,9 @@ var gVar = 5; | |
| alert(window.gVar); // 5 (var로 선언한 변수는 전역 객체 window의 프로퍼티가 됩니다) | ||
| ``` | ||
|
|
||
| <<<<<<< HEAD | ||
| 하위 호환성 때문에 이런 방식으로 전역 객체를 사용해도 동작은 하지만, 이 방법은 쓰지 않으시길 바랍니다. [모듈](info:modules)을 사용하는 모던 자바스크립트는 이런 방식을 지원하지 않습니다. | ||
| ======= | ||
| Function declarations have the same effect (statements with `function` keyword in the main code flow, not function expressions). | ||
| 함수 선언문도 동일하게 동작합니다(함수 표현식이 아닌 메인 코드 흐름의 'function' 키워드를 사용한 문장에 한합니다). | ||
|
|
||
| Please don't rely on that! This behavior exists for compatibility reasons. Modern scripts use [JavaScript modules](info:modules) where such a thing doesn't happen. | ||
| >>>>>>> upstream/master | ||
| 하위 호환성 때문에 이런 방식으로 전역 객체를 사용해도 동작은 하지만, 이 방법은 쓰지 않으시길 바랍니다. [모듈](info:modules)을 사용하는 모던 자바스크립트는 이런 방식을 지원하지 않습니다. | ||
|
|
||
| `var` 대신 `let`을 사용하면 위 예시와는 달리 전역 객체를 통해 변수에 접근할 수 없습니다. | ||
|
|
||
|
|
@@ -91,14 +83,7 @@ if (!window.Promise) { | |
| 전역 객체엔 `Array`와 같은 내장 객체, `window.innerHeight`(뷰포트의 높이를 반환함)같은 브라우저 환경 전용 변수 등이 저장되어 있습니다. | ||
| - 전역 객체는 `globalThis`라는 보편적인 이름으로 불립니다. | ||
|
|
||
| <<<<<<< HEAD | ||
| 하지만 '관습'에 따라 브라우저에서는 `window`, Node.js에서는 `global`이라는 이름으로 불릴 때가 많습니다. `globalThis`는 제안 목록에 추가 된 지 얼마 안 된 기능이기 때문에, 비 크로미움 기반 브라우저에선 지원하지 않습니다(폴리필을 구현하면 사용할 수 있습니다). | ||
| 하지만 '관습'에 따라 브라우저에서는 `window`, Node.js에서는 `global`이라는 이름으로 불릴 때가 많습니다. | ||
| - 프로젝트 전체에서 꼭 필요한 변수만 전역 객체에 저장하도록 하고, 전역 변수는 가능한 한 최소한으로 사용합시다. | ||
| - [모듈](info:modules)을 사용하고 있지 않다면, 브라우저에서 `var`로 선언한 전역 변수는 전역 객체의 프로퍼티가 됩니다. | ||
| - 이해하기 쉽고 요구사항 변경에 쉽게 대응할 수 있는 코드를 구현하려면, `window.x`처럼 전역 객체의 프로퍼티에 직접 접근합시다. | ||
| ======= | ||
| ...But more often is referred by "old-school" environment-specific names, such as `window` (browser) and `global` (Node.js). | ||
|
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. 번역이 잘못된 것 같습니다. |
||
| - We should store values in the global object only if they're truly global for our project. And keep their number at minimum. | ||
| - In-browser, unless we're using [modules](info:modules), global functions and variables declared with `var` become a property of the global object. | ||
| - To make our code future-proof and easier to understand, we should access properties of the global object directly, as `window.x`. | ||
| >>>>>>> upstream/master | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기 예시가 누락된 것 같아요! army1, army2 부분이요!