prerequisites.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <!DOCTYPE html><html lang="ko"><head>
  2. <meta charset="utf-8">
  3. <title>먼저 알아야 할 것들</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – 먼저 알아야 할 것들">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. <link rel="stylesheet" href="/manual/ko/lang.css">
  21. </head>
  22. <body>
  23. <div class="container">
  24. <div class="lesson-title">
  25. <h1>먼저 알아야 할 것들</h1>
  26. </div>
  27. <div class="lesson">
  28. <div class="lesson-main">
  29. <p>이 시리즈는 Three.js에 입문하는 초심자를 위한 튜토리얼입니다. 이 시리즈는
  30. 독자가 최소한 자바스크립트 기본 프로그래밍에 익숙하며, DOM이 무엇인지 설명할
  31. 수 있고, HTML과 자바스크립트로 DOM 요소를 조작할 수 있다는 전제 하에 작성하였습니다.
  32. 또한 <code class="notranslate" translate="no">&lt;script type="module"&gt;</code> 태그로 <a href="https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/import">ES2015 모듈</a>을
  33. 불러올 수 있으며, <a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Selectors">CSS 셀렉터가 무엇인지</a>도
  34. 알고, ES5, ES2015, 약간의 ES2016도 알며, 브라우저가 자바스크립트를 이벤트와 콜백으로만
  35. 실행한다는 것도 알고, 클로저가 무엇인지까지 안다고 가정했죠.</p>
  36. <p>이번 글에서는 시리즈를 읽는 데 필요한 이런 기본 전제에 대해 간단히 환기하고
  37. 넘어가겠습니다.</p>
  38. <h2 id="es2015-">ES2015 모듈</h2>
  39. <p>ES2015 모듈은 스크립트 안에서 <code class="notranslate" translate="no">import</code> 키워드나, 인라인 <code class="notranslate" translate="no">&lt;script type="module"&gt;</code>
  40. 태그로 불러올 수 있습니다. 두 가지 예시를 동시에 써보죠.</p>
  41. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;script type="module"&gt;
  42. import * as THREE from 'three';
  43. ...
  44. &lt;/script&gt;
  45. </pre>
  46. <p>모듈의 경로는 반드시 상대 경로나 절대 경로여야 합니다. <code class="notranslate" translate="no">&lt;img&gt;</code>, <code class="notranslate" translate="no">&lt;a&gt;</code> 태그, CSS 경로와
  47. 달리 여기서 상대 경로란 <code class="notranslate" translate="no">./</code>이나 <code class="notranslate" translate="no">../</code>로 시작하는 경로를 말합니다.</p>
  48. <p>더 자세한 것은 <a href="fundamentals.html">이 글</a>의 마지막 부분을 참고하세요.</p>
  49. <h2 id="-document-queryselector-document-queryselectorall-"><code class="notranslate" translate="no">document.querySelector</code>와 <code class="notranslate" translate="no">document.querySelectorAll</code></h2>
  50. <p>요소를 선택할 때는 <code class="notranslate" translate="no">document.querySelector</code>나 <code class="notranslate" translate="no">document.querySelectorAll</code>을
  51. 사용하면 됩니다. 첫 번째는 CSS 셀렉터와 일치하는 첫 번째 요소를 반환하고, 두 번째는
  52. CSS 셀렉터와 일치하는 모든 요소를 반환하죠.</p>
  53. <h2 id="-onbody-"><code class="notranslate" translate="no">onbody</code>를 쓰지 마세요</h2>
  54. <p>옛날에 개발된 많은 페이지가 body 태그의 onload 속성을 사용합니다.</p>
  55. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">&lt;body onload="somefunction()"&gt;
  56. </pre><p>이런 스타일은 더 이상 권장하지 않습니다. script 태그를 페이지의 끝에 삽입하세요.</p>
  57. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;html&gt;
  58. &lt;head&gt;
  59. ...
  60. &lt;/head&gt;
  61. &lt;body&gt;
  62. ...
  63. &lt;/body&gt;
  64. &lt;script&gt;
  65. // inline javascript
  66. &lt;/script&gt;
  67. &lt;/html&gt;
  68. </pre>
  69. <p>또는 <a href="https://developer.mozilla.org/ko/docs/Web/HTML/Element/script"><code class="notranslate" translate="no">defer</code> 속성을 사용</a>하는
  70. 것이 좋습니다.</p>
  71. <h2 id="-closure-">클로저(closure)란?</h2>
  72. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function a(v) {
  73. const foo = v;
  74. return function() {
  75. return foo;
  76. };
  77. }
  78. const f = a(123);
  79. const g = a(456);
  80. console.log(f()); // 123 출력
  81. console.log(g()); // 456 출력
  82. </pre>
  83. <p>위 예제에서 함수 <code class="notranslate" translate="no">a</code>는 매번 호출할 때마다 새로운 함수를 반환합니다. 이는 상수 <code class="notranslate" translate="no">foo</code>에
  84. 대한 <em>클로저</em>이죠. 자세한 건 <a href="https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Closures">여기</a>를
  85. 참고하기 바랍니다.</p>
  86. <h2 id="-this-"><code class="notranslate" translate="no">this</code> 이해하기</h2>
  87. <p><code class="notranslate" translate="no">this</code>는 마법이 아닙니다. <code class="notranslate" translate="no">this</code>는 다른 인자들처럼 자동으로 함수에 넘겨지는 일종의 변수이죠.
  88. 간단히 함수를 직접 호출하는 경우를 예로 들어보겠습니다.</p>
  89. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">somefunction(a, b, c);
  90. </pre><p>여기서 <code class="notranslate" translate="no">this</code>는 <code class="notranslate" translate="no">null</code>입니다(엄격(strict) 모드나 모듈 안에서). 반면 <code class="notranslate" translate="no">.</code> 연산자를 붙여 메서드로
  91. 호출하는 경우,</p>
  92. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">someobject.somefunction(a, b, c);
  93. </pre><p><code class="notranslate" translate="no">this</code>는 <code class="notranslate" translate="no">someobject</code>로 지정될 겁니다.</p>
  94. <p>많은 사람들이 헷갈리는 부분은 메서드를 콜백 처리할 때이죠.</p>
  95. <pre class="prettyprint showlinemods notranslate notranslate" translate="no"> const callback = someobject.somefunction;
  96. loader.load(callback);
  97. </pre><p>자바스크립트에 능숙하지 않은 사람이 보기에는 문제가 없을지 모르나, <code class="notranslate" translate="no">loader.load</code>는 콜백 함수를
  98. <code class="notranslate" translate="no">.</code> 연산자를 써서 호출하지 않으므로-loader가 별도로 <code class="notranslate" translate="no">this</code>를 지정하지 않는 한-<code class="notranslate" translate="no">this</code>는 null이
  99. 됩니다. 콜백 함수를 호출할 때 <code class="notranslate" translate="no">this</code>를 <code class="notranslate" translate="no">someobject</code>로 지정하려면 명시적으로 <code class="notranslate" translate="no">this</code>를 종속시켜야(binding)
  100. 합니다.</p>
  101. <pre class="prettyprint showlinemods notranslate notranslate" translate="no"> const callback = someobject.somefunction.bind(someobject);
  102. loader.load(callback);
  103. </pre><p><code class="notranslate" translate="no">this</code>를 이해하기 어렵다면, <a href="https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/this"><em>this</em>에 관한 문서</a>를
  104. 한 번 읽어보길 권합니다.</p>
  105. <h2 id="es5-es2015-es2016">ES5/ES2015/ES2016</h2>
  106. <h3 id="-var-const-let-"><code class="notranslate" translate="no">var</code> 대신 <code class="notranslate" translate="no">const</code>와 <code class="notranslate" translate="no">let</code> 사용하기</h3>
  107. <p><code class="notranslate" translate="no">const</code>와 <code class="notranslate" translate="no">let</code>을 쓸 수 있는 환경에서 <code class="notranslate" translate="no">var</code>를 써야할 이유는 <em>전혀</em> 없고, 지금 <code class="notranslate" translate="no">var</code>를 사용하는
  108. 것은 실력 향상에 전혀 도움이 되지 않습니다. 변수를 재할당할 일이 없을 경우 <code class="notranslate" translate="no">const</code>를 사용하세요.
  109. 변수를 재할당하는 것은 흔치 않은 일이니, 주로 <code class="notranslate" translate="no">const</code>를 더 많이 쓰게 될 겁니다. 변수의 값을 바꿔야
  110. 한다면 <code class="notranslate" translate="no">let</code>을 사용하세요. 이런 습관을 들이면 버그를 훨씬 더 많이 줄일 수 있습니다.</p>
  111. <h3 id="-for-elem-in-collection-for-elem-of-collection-"><code class="notranslate" translate="no">for(elem in collection)</code> 대신 <code class="notranslate" translate="no">for(elem of collection)</code> 사용하기</h3>
  112. <p><code class="notranslate" translate="no">for of</code>는 <code class="notranslate" translate="no">for in</code>의 문제를 해결하기 위해 새로 추가된 문법입니다.</p>
  113. <p>예를 들어 객체의 키/값 쌍을 반복문으로 돌릴 경우 다음과 같이 쓸 수 있죠.</p>
  114. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">for (const [key, value] of Object.entries(someObject)) {
  115. console.log(key, value);
  116. }
  117. </pre>
  118. <h3 id="-foreach-map-filter-"><code class="notranslate" translate="no">forEach</code>, <code class="notranslate" translate="no">map</code>, <code class="notranslate" translate="no">filter</code> 등을 적절히 활용하기</h3>
  119. <p>ES5에서 배열에 <a href="https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach"><code class="notranslate" translate="no">forEach</code></a>,
  120. <a href="https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/map"><code class="notranslate" translate="no">map</code></a>
  121. 메서드 등이 추가되었고, ES2015에서는 <a href="https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/filter"><code class="notranslate" translate="no">filter</code></a>
  122. 메서드 등 여러 유용한 메서드가 추가되었습니다.</p>
  123. <h3 id="-destructuring-">구조분해할당(destructuring) 사용하기</h3>
  124. <p><code class="notranslate" translate="no">const dims = { width: 300, height: 150 }</code>. 이런 객체가 있다고 해보죠. 객체의
  125. 각 속성을 별도의 변수에 할당하고자 합니다.</p>
  126. <p>기존 방법</p>
  127. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const width = dims.width;
  128. const height = dims.height;
  129. </pre>
  130. <p>새로운 방법</p>
  131. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const { width, height } = dims;
  132. </pre>
  133. <p>구조분해할당은 배열에도 적용할 수 있습니다. <code class="notranslate" translate="no">const position = [1, 2, 3, 4]</code>.
  134. 이런 배열이 있다고 해보죠.</p>
  135. <p>기존 방법</p>
  136. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const x = position[2];
  137. const y = position[1];
  138. </pre>
  139. <p>새로운 방법</p>
  140. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const [ , y, x ] = position;
  141. </pre>
  142. <p>또한 매개변수에도 구조분해할당을 적용할 수 있습니다.</p>
  143. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const dims = { width: 300, height: 150 };
  144. const position = [1, 2, 3, 4];
  145. function distFromOrig([x, y]) {
  146. return Math.sqrt(x * x + y * y);
  147. }
  148. const dist = distFromOrig(position); // dist = 2.236...
  149. function area({ width, height }) {
  150. return width * height;
  151. }
  152. const a = area(dims); // a = 45000
  153. </pre>
  154. <h3 id="-">객체 선언 시 축약 문법 사용</h3>
  155. <p>기존 방법</p>
  156. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> const width = 300;
  157. const height = 150;
  158. const obj = {
  159. width: width,
  160. height: height,
  161. area: function() {
  162. return this.width * this.height
  163. },
  164. };
  165. </pre>
  166. <p>새로운 방법</p>
  167. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> const width = 300;
  168. const height = 150;
  169. const obj = {
  170. width,
  171. height,
  172. area() {
  173. return this.width * this.height;
  174. },
  175. };
  176. </pre>
  177. <h3 id="-">전개 연산자 <code class="notranslate" translate="no">...</code> 사용하기</h3>
  178. <p>전개 연산자는 매우 유용합니다. 예를 들어보죠.</p>
  179. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> function log(className, ...args) {
  180. const elem = document.createElement('div');
  181. elem.className = className;
  182. elem.textContent = args.join(' ');
  183. document.body.appendChild(elem);
  184. }
  185. </pre>
  186. <p>또 배열을 인자로 넘겨줄 때도 유용합니다.</p>
  187. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const position = [1, 2, 3];
  188. somemesh.position.set(...position);
  189. </pre>
  190. <p>배열을 얕은 복사할 때 사용할 수도 있고</p>
  191. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const copiedPositionArray = [...position];
  192. copiedPositionArray.push(4); // [1,2,3,4]
  193. console.log(position); // [1,2,3] 기존 배열은 영향을 받지 않음
  194. </pre>
  195. <p>객체를 합칠 때도 사용할 수 있죠.</p>
  196. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">const a = { abc: 123 };
  197. const b = { def: 456 };
  198. const c = { ...a, ...b }; // c = { abc: 123, def: 456 }
  199. </pre><h3 id="-class-"><code class="notranslate" translate="no">class</code> 사용하기</h3>
  200. <p>ES5 이하의 문법으로 클래스 스타일의 객체를 만드는 방법은 다른 개발자들에게 낯선 요소
  201. 중 하나였습니다. ES2015부터는 C++/C#/Java 등 다른 객체지향 언어처럼 <a href="https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Classes"><code class="notranslate" translate="no">class</code> 키워드를 사용</a>해
  202. 클래스를 생성할 수 있습니다.</p>
  203. <h3 id="getter-setter">getter와 setter</h3>
  204. <p>모던 프로그래밍 언어에는 대부분
  205. <a href="https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions/get">getter</a>와
  206. <a href="https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions/set">setter</a>가
  207. 있습니다. ES2015의 <code class="notranslate" translate="no">class</code> 문법을 사용하면 훨씬 쉽게 getter와 setter를 설정할 수 있죠.</p>
  208. <h3 id="-arrow-function-">화살표 함수(arrow function) 활용하기</h3>
  209. <p>화살표 함수는 특히 콜백과 프로미스를 처리할 때 유용합니다.</p>
  210. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">loader.load((texture) =&gt; {
  211. // 불러온 텍스처를 사용
  212. });
  213. </pre>
  214. <p>화살표 함수는 <code class="notranslate" translate="no">this</code> 값을 지정하지 않습니다.</p>
  215. <p>(※ 원문에서는 "Arrow functions bind <code class="notranslate" translate="no">this</code>.(화살표 함수는 <code class="notranslate" translate="no">this</code>를 바인딩한다.)"라고 적었지만,
  216. 표준에 기재된 바 화살표 함수는 익명 함수로 <code class="notranslate" translate="no">this</code>, <code class="notranslate" translate="no">arguments</code>, <code class="notranslate" translate="no">super</code> 또는 <code class="notranslate" translate="no">new.target</code>을 지정하지
  217. 않습니다. 혼돈을 막기 위해 달리 번역하였으니 참고 바랍니다. 역주. 출처: <a href="https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions/%EC%95%A0%EB%A1%9C%EC%9A%B0_%ED%8E%91%EC%85%98">MDN</a>)</p>
  218. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const foo = (args) =&gt; {/* code */};
  219. </pre>
  220. <p>위 예제는 다음과 과 같죠.</p>
  221. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const foo = (function(args) {/* code */}).bind(this));
  222. </pre>
  223. <h3 id="-promise-async-await">프로미스(Promise)와 async/await</h3>
  224. <p>프로미스는 비동기 처리를 도와줍니다. async/await는 프로미스를 좀 더 쉽게 쓰도록 도와주죠.</p>
  225. <p>이 둘은 짧게 다루기 어려우므로 다른 글(<a href="https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Using_promises">프로미스</a>,
  226. <a href="https://developer.mozilla.org/ko/docs/Learn/JavaScript/Asynchronous/Async_await">async/await</a>)을
  227. 참고하기 바랍니다.</p>
  228. <h3 id="-template-literals-">템플릿 리터럴(Template Literals)</h3>
  229. <p>템플릿 리터럴은 따옴표 대신 백틱(backticks, 억음 부호)을 사용한 문자열입니다.</p>
  230. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">const foo = `템플릿 리터럴 문자열입니다`;
  231. </pre><p>템플릿 리터럴은 문자열에 2가지 기능을 더한 것인데요. 하나는 여러 줄에 걸쳐 쓸 수
  232. 있는 기능입니다.</p>
  233. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const foo = `템플릿
  234. 리터럴
  235. 문자열입니다`;
  236. const bar = "템플릿\n리터럴\n문자열입니다";
  237. </pre>
  238. <p>예제의 <code class="notranslate" translate="no">foo</code>와 <code class="notranslate" translate="no">bar</code>는 같은 문자열이죠.</p>
  239. <p>다른 하나는 문자열 중간에 자바스크립트 표현식을 끼워넣을 수 있는 기능으로,
  240. <code class="notranslate" translate="no">${ 자바스크립트 표현식 }</code>처럼 사용합니다.</p>
  241. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const r = 192;
  242. const g = 255;
  243. const b = 64;
  244. const rgbCSSColor = `rgb(${r},${g},${b})`;
  245. </pre>
  246. <p>함수를 실행하거나</p>
  247. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = [ 192, 255, 64 ];
  248. const rgbCSSColor = `rgb(${ color.join(',') })`;
  249. </pre>
  250. <p>계산식을 넣을 수도 있습니다.</p>
  251. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const aWidth = 10;
  252. const bWidth = 20;
  253. someElement.style.width = `${ aWidth + bWidth }px`;
  254. </pre>
  255. <h1 id="-coding-convection-">자바스크립트 네이밍 컨벤션(coding convection, 코딩 스타일 규약) 지키기</h1>
  256. <p>코드를 작성하는 것은 여러분의 자유지만, 웬만하면 한 가지 규약은 지키는 것이 좋습니다.
  257. 자바스크립트의 변수, 메서드, 함수 이름은 전부 lowerCamelCase이죠. 생성자나, 클래스의
  258. 이름은 UpperCamelCase입니다. 이 규약만 따른다면 자바스크립트의 세계에서 크게 문제될
  259. 것은 없죠. 대부분의 <a href="https://eslint.org">린터(linter)</a>나 린팅 프로그램이 위 네이밍
  260. 컨벤션을 지키지 않는 코드에 대해 에러나 경고를 던집니다.</p>
  261. <p>(※ 문서에 따라 lower camel case는 camelCase, upper camel case는 PascalCase로 부르기도
  262. 합니다. 역주.)</p>
  263. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const v = new vector(); // 모든 클래스가 대문자로 시작할 경우 에러
  264. const v = Vector(); // 모든 함수가 소문자로 시작할 경우 에러
  265. </pre>
  266. <h1 id="visual-studio-code-">Visual Studio Code 사용해보기</h1>
  267. <p>이 세상에는 훌륭한 에디터가 많습니다. 자신이 좋아하는 에디터를 쓰는 것이 가장
  268. 좋죠. 하지만 아직 마음에 드는 자바스크립트 에디터가 없다면 <a href="https://code.visualstudio.com/">Visual Studio Code</a>를
  269. 써보세요! 에디터를 설치하고 <a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint">eslint를 설정</a>
  270. 하면-환경에 따라 시간이 좀 걸릴지 모르나-무수한 자바스크립트 코드의 버그를
  271. 잡는 데 도움이 될 겁니다.</p>
  272. <p>예를 들어 <a href="https://eslint.org/docs/rules/no-undef"><code class="notranslate" translate="no">no-undef</code> 규칙</a>을 활성화하면
  273. VSCode는 ESLint를 통해 변수의 값이 지정되지 않은 경우 경고를 출력할 겁니다.</p>
  274. <div class="threejs_center"><img style="width: 615px;" src="../resources/images/vscode-eslint-not-defined.png"></div>
  275. <p>위 예시에서 작성자는 <code class="notranslate" translate="no">doTheThing</code> 함수를 <code class="notranslate" translate="no">doThing</code>로 잘못 적었습니다. 그러자 <code class="notranslate" translate="no">doThing</code>
  276. 아래에 밑줄이 생겼죠. 그리고 그 위에 커서를 올리면 해당 함수를 선언하지 않았다고
  277. 알려줍니다. 미리 에러를 하나 줄인 셈이죠.</p>
  278. <p><code class="notranslate" translate="no">THREE</code>를 쓸 때도 경고가 나타날 것이므로 코드 상단에 <code class="notranslate" translate="no">/* global THREE */</code>를 선언해
  279. <code class="notranslate" translate="no">THREE</code>가 미리 선언되었음을 알려줄 수 있습니다.</p>
  280. <div class="threejs_center"><img style="width: 615px;" src="../resources/images/vscode-eslint-not-a-constructor.png"></div>
  281. <p>위 이미지에서 ESLint는 <code class="notranslate" translate="no">대문자로 시작하는 이름</code>은 생성자이니 <code class="notranslate" translate="no">new</code> 키워드를 사용하라고
  282. 알려줍니다. <a href="https://eslint.org/docs/rules/new-cap"><code class="notranslate" translate="no">new-cap</code> 규칙</a> 덕에 에러를 하나
  283. 더 줄였네요.</p>
  284. <p>이 외에도 <a href="https://eslint.org/docs/rules/">설정할 수 있는 규칙</a>이 거의 100개 정도 더
  285. 있습니다. 아까 <code class="notranslate" translate="no">var</code> 대신 <code class="notranslate" translate="no">const</code>와 <code class="notranslate" translate="no">let</code>을 쓰라고 했었죠.</p>
  286. <p>아래 이미지에서 작성자는 <code class="notranslate" translate="no">var</code>를 썼는데, <code class="notranslate" translate="no">let</code>이나 <code class="notranslate" translate="no">const</code>를 쓰라는 경고를 받았습니다.</p>
  287. <div class="threejs_center"><img style="width: 615px;" src="../resources/images/vscode-eslint-var.png"></div>
  288. <p>또 <code class="notranslate" translate="no">let</code>을 쓰고 재할당을 하지 않자 <code class="notranslate" translate="no">const</code>를 쓰라고 제안합니다.</p>
  289. <div class="threejs_center"><img style="width: 615px;" src="../resources/images/vscode-eslint-let.png"></div>
  290. <p>물론 <code class="notranslate" translate="no">var</code>가 더 좋다고 생각하면 이 규칙을 꺼버리면 됩니다. 이 규칙을 쓰는 건 제가 성능과
  291. 버그 예방 면에서 <code class="notranslate" translate="no">var</code>보다 <code class="notranslate" translate="no">let</code>과 <code class="notranslate" translate="no">const</code>를 쓰는 걸 더 좋아하기 때문이죠.</p>
  292. <p>일부 파일이나 코드의 일부분에서 규칙을 덮어 씌워야 할 경우, <a href="https://eslint.org/docs/user-guide/configuring#disabling-rules-with-inline-comments">주석을 추가해 규칙을 끌 수
  293. 있습니다</a>.</p>
  294. <h1 id="-">구형 브라우저를 지원해야 한다면 트랜스파일러를 쓰세요</h1>
  295. <p>대부분의 모던 브라우저가 자동 업데이트 기능을 사용하기에 최신 문법을 사용해 생산성과
  296. 버그 예방 두 마리의 토끼를 동시에 잡을 수 있습니다. 만약 꼭 구형 브라우저를 지원해야
  297. 한다면 <a href="https://babeljs.io">ES5/ES2015 등의 최신 문법을 구형 문법으로 변환시켜주는 트랜스파일러</a>를
  298. 사용하길 권장합니다.</p>
  299. </div>
  300. </div>
  301. </div>
  302. <script src="../resources/prettify.js"></script>
  303. <script src="../resources/lesson.js"></script>
  304. </body></html>