prerequisites.html 17 KB

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