prerequisites.html 16 KB

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