prerequisites.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <!DOCTYPE html><html lang="zh"><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="/manual/resources/lesson.css">
  12. <link rel="stylesheet" href="/manual/resources/lang.css">
  13. <link rel="stylesheet" href="/manual/zh/lang.css">
  14. </head>
  15. <body>
  16. <div class="container">
  17. <div class="lesson-title">
  18. <h1>先决条件</h1>
  19. </div>
  20. <div class="lesson">
  21. <div class="lesson-main">
  22. <p>这些文章意在帮助你学习如何使用three.js。
  23. 假设你知道怎么使用JavaScript编程。假设
  24. 你知道DOM是什么,怎么写HTML以及使用JavaScript创建
  25. DOM元素。假设你知道如何使用 <code class="notranslate" translate="no">&lt;script&gt;</code>标签来
  26. 引用外部的JavaScript文件以及行内脚本。
  27. 假设你了解CSS并且知道
  28. <a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Selectors">CSS选择器</a>.
  29. 还假设你了解ES5、 ES6或者一些ES7。
  30. 假设你知道浏览器通过事件和回调函数来运行JavaScript。
  31. 假设你知道什么是闭包。</p>
  32. <p>这有一些简单的复习和笔记。</p>
  33. <h2 id="-document-queryselector-and-document-queryselectorall-"><code class="notranslate" translate="no">document.querySelector</code> and <code class="notranslate" translate="no">document.querySelectorAll</code></h2>
  34. <p>你可以使用<code class="notranslate" translate="no">document.querySelector</code>来选择和CSS选择器
  35. 匹配的第一个元素。 <code class="notranslate" translate="no">document.querySelectorAll</code>返回
  36. 所有和CSS选择器匹配的元素。</p>
  37. <h2 id="you-don-t-need-onbody-">You don't need <code class="notranslate" translate="no">onbody</code></h2>
  38. <p>很多20年前的页面像这样使用HTML</p>
  39. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">&lt;body onload="somefunction()"&gt;
  40. </pre><p>这种风格已经被弃用了。将你的脚本放在
  41. 页面的底部。</p>
  42. <pre class="prettyprint showlinemods notranslate lang-html" translate="no">&lt;html&gt;
  43. &lt;head&gt;
  44. ...
  45. &lt;/head&gt;
  46. &lt;body&gt;
  47. ...
  48. &lt;/body&gt;
  49. &lt;script&gt;
  50. // inline javascript
  51. &lt;/script&gt;
  52. &lt;/html&gt;
  53. </pre>
  54. <p>or <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script">use the <code class="notranslate" translate="no">defer</code> property</a>.</p>
  55. <h2 id="-">了解闭包如何工作</h2>
  56. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function a(v) {
  57. const foo = v;
  58. return function() {
  59. return foo;
  60. };
  61. }
  62. const f = a(123);
  63. const g = a(456);
  64. console.log(f()); // prints 123
  65. console.log(g()); // prints 456
  66. </pre>
  67. <p>在上面的代码中函数<code class="notranslate" translate="no">a</code>每次被调用都会创建一个新的函数。新函数
  68. 会封存变量<code class="notranslate" translate="no">foo</code>. 这里有 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures">更多信息</a>.</p>
  69. <h2 id="-this-">理解<code class="notranslate" translate="no">this</code>的工作原理</h2>
  70. <p><code class="notranslate" translate="no">this</code>并不是什么魔法。它实际上像是一个像被自动传给函数的参数一样的变量。
  71. 简单的说就是像这样直接调用函数</p>
  72. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">somefunction(a, b, c);
  73. </pre><p><code class="notranslate" translate="no">this</code>将会为<code class="notranslate" translate="no">null</code> (使用严格模式时) 当你使用<code class="notranslate" translate="no">.</code>操作符像这样调用函数时</p>
  74. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">someobject.somefunction(a, b, c);
  75. </pre><p><code class="notranslate" translate="no">this</code>将会被设置为<code class="notranslate" translate="no">someobject</code>。</p>
  76. <p>令人困惑的部分是使用回调函数。</p>
  77. <pre class="prettyprint showlinemods notranslate notranslate" translate="no"> const callback = someobject.somefunction;
  78. loader.load(callback);
  79. </pre><p>并没有像不熟悉的所期望的那样工作,因为当
  80. <code class="notranslate" translate="no">loader.load</code>调用回调函数时并没有使用<code class="notranslate" translate="no">.</code>操作符。
  81. 所以默认<code class="notranslate" translate="no">this</code>将会为null (除非loader明确将他设置为某些东西)。
  82. 如果你希望<code class="notranslate" translate="no">this</code>为<code class="notranslate" translate="no">someobject</code>当回调函数执行时你需要
  83. 通过将this绑定到函数来告诉JavaScript。</p>
  84. <pre class="prettyprint showlinemods notranslate notranslate" translate="no"> const callback = someobject.somefunction.bind(someobject);
  85. loader.load(callback);
  86. </pre><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this"><em>this</em> article might help explain <code class="notranslate" translate="no">this</code></a>.</p>
  87. <h2 id="es5-es6-es7-">ES5/ES6/ES7 特性</h2>
  88. <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>
  89. <p>没有<strong>任何</strong>理由再使用<code class="notranslate" translate="no">var</code>,基于此使用它被认为是
  90. 坏习惯。大所数时间如果变量不会被重新分配使用<code class="notranslate" translate="no">const</code>。
  91. 变量会改变的情况下使用<code class="notranslate" translate="no">let</code>。这将会帮助避免大量bug。</p>
  92. <h3 id="-for-elem-of-collection-for-elem-in-collection-">使用<code class="notranslate" translate="no">for(elem of collection)</code>而不是<code class="notranslate" translate="no">for(elem in collection)</code></h3>
  93. <p><code class="notranslate" translate="no">for of</code>是新的,<code class="notranslate" translate="no">for in</code>是旧的。 <code class="notranslate" translate="no">for of</code>解决了<code class="notranslate" translate="no">for in</code>的问题。</p>
  94. <p>举个例子,你可以像这样迭代一个对象的所以键/值对</p>
  95. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">for (const [key, value] of Object.entries(someObject)) {
  96. console.log(key, value);
  97. }
  98. </pre>
  99. <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>
  100. <p>数组新增的函数<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach"><code class="notranslate" translate="no">forEach</code></a>、
  101. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"><code class="notranslate" translate="no">map</code></a>和
  102. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter"><code class="notranslate" translate="no">filter</code></a>
  103. 在现代JavaScript中使用都是相当广泛的。</p>
  104. <h3 id="-">使用解构赋值</h3>
  105. <p>假设有一个对象<code class="notranslate" translate="no">const dims = {width: 300, height: 150}</code></p>
  106. <p>老的代码</p>
  107. <pre class="prettyprint showlinemods notranslate notranslate" translate="no"> const width = dims.width;
  108. const height = dims.height;
  109. </pre><p>新代码</p>
  110. <pre class="prettyprint showlinemods notranslate notranslate" translate="no"> const {width, height} = dims;
  111. </pre><h3 id="-">使用对象声明简写</h3>
  112. <p>老的代码</p>
  113. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> const width = 300;
  114. const height = 150;
  115. const obj = {
  116. width: width,
  117. height: height,
  118. area: function() {
  119. return this.width * this.height
  120. },
  121. };
  122. </pre>
  123. <p>新代码</p>
  124. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> const width = 300;
  125. const height = 150;
  126. const obj = {
  127. width,
  128. height,
  129. area() {
  130. return this.width * this.height;
  131. },
  132. };
  133. </pre>
  134. <h3 id="-">使用扩展运算符<code class="notranslate" translate="no">...</code></h3>
  135. <p>扩展运算符有大量的用途。例如</p>
  136. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> function log(className, ...args) {
  137. const elem = document.createElement('div');
  138. elem.className = className;
  139. elem.textContent = [...args].join(' ');
  140. document.body.appendChild(elem);
  141. }
  142. </pre>
  143. <p>另一个例子</p>
  144. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const position = [1, 2, 3];
  145. somemesh.position.set(...position);
  146. </pre>
  147. <h3 id="-class-">使用<code class="notranslate" translate="no">class</code></h3>
  148. <p>大多数人都不熟悉在ES5之前生成类对象的语法。
  149. ES5之后你现在可以<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes">使用<code class="notranslate" translate="no">class</code>
  150. 关键字</a>
  151. 接近于C++/C#/Java的语法。</p>
  152. <h3 id="-getters-setters">理解 getters 和 setters</h3>
  153. <p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get">Getters</a>和
  154. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set">setters</a>是
  155. 在大多数现代语言中常见的。ES6<code class="notranslate" translate="no">class</code>语法
  156. 让他们比ES6之前的更容易。</p>
  157. <h3 id="-">合理使用箭头函数</h3>
  158. <p>回调函数和promise使用箭头函数非常有用</p>
  159. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">loader.load((texture) =&gt; {
  160. // use textrue
  161. });
  162. </pre>
  163. <p>箭头函数会绑定<code class="notranslate" translate="no">this</code>,它是下面的简写</p>
  164. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">(function(args) {/* code */}).bind(this))
  165. </pre>
  166. <h3 id="promises-async-await">Promises 以及 async/await</h3>
  167. <p>Promises改善异步代码的处理。Async/await改善
  168. promise的使用。</p>
  169. <p>这是一个需要深入了解的话题你可以<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises">在这里
  170. 细读promises</a>
  171. 和<a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await">async/await</a>.</p>
  172. <h3 id="-">使用模板字符串</h3>
  173. <p>模板字符串是使用反引号代替引号的字符串。</p>
  174. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">const foo = `this is a template literal`;
  175. </pre><p>模板字符串有两个基本的特点。一个是它可以多行</p>
  176. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const foo = `this
  177. is
  178. a
  179. template
  180. literal`;
  181. const bar = "this\nis\na\ntemplate\nliteral";
  182. </pre>
  183. <p>上面的<code class="notranslate" translate="no">foo</code>和<code class="notranslate" translate="no">bar</code>是一样的。</p>
  184. <p>另一个是你可以超越字符串模式
  185. 使用<code class="notranslate" translate="no">${javascript表达式}</code>插入JavaScript代码段。这是模板部分。比如:</p>
  186. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const r = 192;
  187. const g = 255;
  188. const b = 64;
  189. const rgbCSSColor = `rgb(${r},${g},${b})`;
  190. </pre>
  191. <p>or</p>
  192. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = [192, 255, 64];
  193. const rgbCSSColor = `rgb(${color.join(',')})`;
  194. </pre>
  195. <p>or</p>
  196. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const aWidth = 10;
  197. const bWidth = 20;
  198. someElement.style.width = `${aWidth + bWidth}px`;
  199. </pre>
  200. <h1 id="-javascript-">学习JavaScript代码风格。</h1>
  201. <p>尽管欢迎您以任何方式组织您的代码,但至少有一个约定您应该知道。
  202. 在JavaScript中变量、函数名、方法名
  203. 都是小驼峰。构造函数、类名都是
  204. 大驼峰。如果你遵守这些约定你的diamagnetic将会和大部分JavaScript匹配。</p>
  205. <h1 id="-visual-studio-code">考虑使用Visual Studio Code</h1>
  206. <p>当然你想想用什么编辑器就用什么但是如果你没尝试过它那就考虑下
  207. 使用<a href="https://code.visualstudio.com/">Visual Studio Code</a>来写JavaScript。
  208. 安装完之后<a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint">设置
  209. eslint</a>。
  210. 可能会花几分钟来设置但是会对你寻找JavaScript的bug有极大的帮助。</p>
  211. <p>一些例子</p>
  212. <p>如果你开启<a href="https://eslint.org/docs/rules/no-undef"><code class="notranslate" translate="no">no-undef</code>规则</a>然后
  213. VSCode通过ESLint将会警告你很多没有定义的变量。 </p>
  214. <div class="threejs_center"><img style="width: 615px;" src="../resources/images/vscode-eslint-not-defined.png"></div>
  215. <p>上面你可以看到我将<code class="notranslate" translate="no">doTheThing</code>误写成了<code class="notranslate" translate="no">doThing</code>。有一个红色的曲线
  216. 在<code class="notranslate" translate="no">doThing</code>下面并且鼠标悬停会提醒我们它未定义。这样就避免了一个错误。</p>
  217. <p>使用<code class="notranslate" translate="no">THREE</code>会得到警告所以将<code class="notranslate" translate="no">/* global THREE */</code>放在你的
  218. JavaScript文件的顶部来告诉eslint<code class="notranslate" translate="no">THREE</code>的存在。</p>
  219. <div class="threejs_center"><img style="width: 615px;" src="../resources/images/vscode-eslint-not-a-constructor.png"></div>
  220. <p>上面你可以看到eslint知道使用<code class="notranslate" translate="no">UpperCaseNames</code>规则的是构造函数
  221. 所以你应该使用<code class="notranslate" translate="no">new</code>操作符。另一个错误被捕捉并避免了。这是<a href="https://eslint.org/docs/rules/new-cap">the
  222. <code class="notranslate" translate="no">new-cap</code>规则</a>。</p>
  223. <p>这里有<a href="https://eslint.org/docs/rules/">100多条规则你可以打开或者关闭或者自定义
  224. </a>。比如上面我提醒你
  225. 应该使用<code class="notranslate" translate="no">const</code>和<code class="notranslate" translate="no">let</code>代替<code class="notranslate" translate="no">var</code>。</p>
  226. <p>这里我使用了<code class="notranslate" translate="no">var</code>它警告我应该使用<code class="notranslate" translate="no">let</code>或者<code class="notranslate" translate="no">const</code></p>
  227. <div class="threejs_center"><img style="width: 615px;" src="../resources/images/vscode-eslint-var.png"></div>
  228. <p>这里我是用了<code class="notranslate" translate="no">let</code>但是它发现我一直没改变值所以建议我使<code class="notranslate" translate="no">const</code>。</p>
  229. <div class="threejs_center"><img style="width: 615px;" src="../resources/images/vscode-eslint-let.png"></div>
  230. <p>当然如果你更希望继续使用<code class="notranslate" translate="no">var</code>你只要关闭那条规则。
  231. 如我上面所说所以我更喜欢使用<code class="notranslate" translate="no">const</code>和<code class="notranslate" translate="no">let</code>而不是<code class="notranslate" translate="no">var</code>因为
  232. 他们工作的更好而且能减少bugs。</p>
  233. <p>对于确实需要重写这些规则的情况<a href="https://eslint.org/docs/user-guide/configuring#disabling-rules-with-inline-comments">你可以添加注释
  234. 来禁用
  235. 他们</a>
  236. 通过单行或者一段代码。</p>
  237. <h1 id="-">如果你确实需要支持老的浏览器请使用编译器</h1>
  238. <p>大多数现代浏览器都是自动更新的,所以使用这些新的特性会帮助你提高效率
  239. 和避免bug。意思是说,如果你正在做一个需要支持老的浏览器的项目,
  240. <a href="https://babeljs.io">有工具会把ES5/ES6/ES7代码
  241. 转换成ES5之前的Javascript</a>.</p>
  242. </div>
  243. </div>
  244. </div>
  245. <script src="/manual/resources/prettify.js"></script>
  246. <script src="/manual/resources/lesson.js"></script>
  247. </body></html>