TypeScript 入门指南 【大白话】
<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; padding: 0 10px; line-height: 1.6; word-spacing: 0; letter-spacing: 0; word-wrap: break-word; text-align: left; color: rgba(51, 51, 51, 1); font-family: -apple-system, system-ui, BlinkMacSystemFont, Helvetica Neue, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif"><h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">前言</span><span class="suffix"></span></h2><h3 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">聊聊为何要学习TypeScript?</span><span class="suffix" style="display: none"></span></h3>
<ol data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">从开发角度来讲, <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">TypeScript</code> 作为强类型语言,对属性有类型约束。在日常开发中少了减少了不必要的因参数类型造成的<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">BUG</code>,当你在使用同事封装好的函数时,类型不知道怎么传递,在开发<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">TS</code> 时,它会有很友好类型的提示,此时你就知道该函数需要传递哪些参数,并且参数类型是什么类型。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">从项目结构来讲, 使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">TypeScript</code> 可以很好的管控项目,通过建立各个模块的类型管理,通过<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">interface</code> 或者 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">类型断言</code> 来管理项目中类型,在对应的模块使用类型,当项目需要迭代更新时,只需要进行对对应的模块类型进行更改。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">从前端全局观来讲,社区三大框架早已结合<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">TypeScript</code> 了, 社区也很完善,已经过了 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">该不该学习TypeScript 的阶段了</code>。</section></li></ol>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400">去年还有很多朋友犹豫用不用学习<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code> , 很多被社区朋友发的文章误导, <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code> 就是 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">AnyScript</code>。</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a8836c6231db4bd094394ad433bc29bb~tplv-k3u1fbpfcp-watermark.image"><figcaption style="text-align: center; color: rgba(136, 136, 136, 1); display: none; opacity: 0.6; margin-top: 12px; font-size: 12px">2.png</figcaption></figure>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400"><code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code> 对没有接触过 强类型语言的朋友有点学习成本,而且在使用<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code> 进行开发时,前期可能代码会很臃肿,让你觉得看起来有很多 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">无用的代码</code> , 不过在后期,你就可以感觉到 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code> 给你带来的乐趣了。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 16px 0 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400">学会 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code>也对你在职场竞争有点优势,在跳槽时,假如你已经使用 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code> 结合 框架 做过一些项目,面试官也会优先考虑你的,薪水从而也提升了。
前端之路还有很长,新的技术/框架更新频率很快, 最终还是离不开 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">JavaScript</code>。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 16px 0 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400">下面,我们一起来看看 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code>,本文是对标 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">TypeScript</code> 文档进行来讲解,更加大白话的简单讲述<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">如何使用TypeScript</code> .</p>
<h3 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">入手导图</span><span class="suffix" style="display: none"></span></h3>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6868ec8168ea47d9ae2b2e5310576c39~tplv-k3u1fbpfcp-zoom-1.image"><figcaption style="text-align: center; opacity: 0.6; font-size: 12px; display: flex; justify-content: center; align-items: center; width: 100%; margin-top: -35px; background: rgba(0, 0, 0, 0.7); color: rgba(255, 255, 255, 1); line-height: 35px; z-index: 20">reWaP1.png</figcaption></figure>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">TypeScript</span><span class="suffix"></span></h2>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">一,安装环境</span><span class="suffix"></span></h2>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#npm install -g typescript<br></code></pre>
<h3 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">1.1 VSCode 配置自动编译文件</span><span class="suffix" style="display: none"></span></h3>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1.</span> 在目录下 <br> tsc --init 自动生成 tsconfig.json <br> tsconfig.json 下 outdir 是输出的路径<br>#<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2.</span> 任务--- 运行任务 监视 tsconfig.json<br><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">二,基本语法</span><span class="suffix"></span></h2>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">2.1 数组</span><span class="suffix" style="display: none"></span></h4>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">定义使用</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 第一种定义方法 let 数组名:类型[] = []</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">var</span> arr:<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">number</span>[] = [<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">3</span>];<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(arr);<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 第二种定义方法 let 数组名:Array[类型] = []</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">var</span> newArr:<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">Array</span><<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">number</span>> = [<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">3</span>];<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(newArr)<br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">2.2 元组</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">它表示 已经 元素的个数和元素类型的数组,各个元素类型可以不一样。</p>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">访问元组长度 和 元素</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">var</span> strArr: = [<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">22</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span>,<span class="hljs-literal" style="color: rgba(1, 132, 187, 1); line-height: 26px">false</span>]<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(strArr.length)<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(strArr[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">0</span>])<br>#它只能按类型的优先顺序输入内容,否则报错<br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">2.3 枚举 enum</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2"><code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">enum</code>类型是对JavaScript标准数据类型的一个补充。</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">如果没有给枚举指定索引的话,默认为 0 , 通过<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">枚举对象[索引]</code>可以获取值</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">如果指定了枚举索引为字符串的话,通过<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">枚举.属性</code>获取的 它的值</section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">enum</span> Sex {Man,Woman}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> m:Sex = Sex.Man;<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(m) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//0</span><br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> w: <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">string</span> = Sex[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1</span>]<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(w) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//Woman</span><br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">enum</span> Animal {Dog = <span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">3</span>, Cat, Tiger};<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(Animal[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">5</span>]) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//Tiger</span><br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">enum</span> info {student = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'学生'</span>, teacher = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'教师'</span>, parent = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'家长'</span> };<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(info.teacher) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//教师</span><br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">2.4 任意类型 any</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">any 为 任意类型,一般在获取dom 使用</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 任意类型</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> newArrs:<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">any</span> = [<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试不同数据 '</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">222</span>,<span class="hljs-literal" style="color: rgba(1, 132, 187, 1); line-height: 26px">false</span>]<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(newArrs)<br># 输出结果为[ <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试不同数据 '</span>, <span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">222</span>, <span class="hljs-literal" style="color: rgba(1, 132, 187, 1); line-height: 26px">false</span> ]<br># 使用场景: 当你不知道类型 或 一个对象 或数据 需要多个类型时,使用<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">any</span><br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content"><em style="font-style: italic; color: rgba(0, 0, 0, 1)">2.5 undefined 类型</em></span><span class="suffix" style="display: none"></span></h4>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> num:<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">number</span> | <span class="hljs-literal" style="color: rgba(1, 132, 187, 1); line-height: 26px">undefined</span> ;<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(num) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 输出 undefined, 但是不会报错</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> newNum:<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">number</span> | <span class="hljs-literal" style="color: rgba(1, 132, 187, 1); line-height: 26px">undefined</span> = <span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">33</span>;<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(newNum) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 输出 33 </span><br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">2.6 never 类型</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">never 代表不存在的值类型,常用作为 抛出异常或者 无限循环的函数返回类型</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"># 应用场景 <br> #<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1.</span> 抛错误<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> errDate = (message:string): <span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">never</span> =></span> {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">throw</span> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">Error</span>(message)<br> }<br> #<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2.</span> 死循环<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> date_for = (): <span class="hljs-function" style="line-height: 26px"><span class="hljs-params" style="line-height: 26px">never</span> =></span> {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">while</span>(<span class="hljs-literal" style="color: rgba(1, 132, 187, 1); line-height: 26px">true</span>) {}<br> }<br> <br># never 类型是任意类型的子类型,没有类型是never 的子类型<br>别的类型不能赋值给never类型, 而 never 类型可以赋值给任意类型<br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">2.7 void 类型</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">void为 函数没有类型,一般用在没有返回值的函数</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"># 如果方法类型为<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">number</span>, 则必须返回内容, 内容且必须为数字<br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">add</span>(<span class="hljs-params" style="line-height: 26px"></span>):<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">number</span></span>{<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">return</span> <span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2323</span>;<br>}<br><br># 如果方法类型为<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">void</span>,不需要返回内容<br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getAdd</span>(<span class="hljs-params" style="line-height: 26px"></span>):<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">void</span></span>{<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span>)<br>}<br><br># 如果方法类型为<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">any</span>,则可以返回任意类型<br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getAny</span>(<span class="hljs-params" style="line-height: 26px"></span>):<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">any</span></span>{<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">return</span> <span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">999</span> + <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'Hello TypeScript'</span><br>}<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(getAny())<span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//999 'Hello TypeScript'</span><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">三,类型断言</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">什么是类型断言?</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">有时候你在定义一个变量时,起初是不知道是什么类型,但在使用过程中知道是什么类型,这时就会用到类型断言了。</section></li></ul>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">3.1第一种写法 尖括号</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> str = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> resLength : number = (<span class="xml" style="line-height: 26px"><span class="hljs-tag" style="line-height: 26px"><<span class="hljs-name" style="color: rgba(228, 86, 73, 1); line-height: 26px">string</span>></span>str).length<br></span></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">3.2第二种写法as</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> str = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> resLength : number = (str <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">as</span> string).length<br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">四,接口</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">TypeScript的核心原则之一是对值所具有的<em style="font-style: italic; color: rgba(0, 0, 0, 1)">结构</em>进行类型检查。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">验证类型时,顺序不影响验证。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">简单的来说,它是类型约束的定义,当你使用这个定义接口时,它会一一匹对接口中定义的类型。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">只要不满足接口中的任何一个属性,都不会通过的。</p>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">4.1 接口可选属性</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">有时候,接口属性不是必须全部需要的,满足某些条件才会需要,这时,就可以采用可选属性</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">格式 : <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">属性 ?: 类型</code></p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">interface Login{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">userName</span>: string,<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">password</span>: string,<br> auth ?: string<br>}<br><br><br><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getLogin</span>(<span class="hljs-params" style="line-height: 26px">obj: Login</span>) </span>{<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">if</span>(obj.auth == <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'管理员'</span>) {<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'可以查看所有菜单'</span>)<br> } <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">else</span> {<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'您的权限比较低,目前不能查看'</span>)<br> }<br>}<br><br><br>getLogin({<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">userName</span>:<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'zhangsanfeng'</span>,<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">password</span>: <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'12121121sd'</span>,<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">auth</span>: <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'管理员'</span><br>}) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//可以查看所有菜单</span><br><br><br>getLogin({<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">userName</span>:<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'zhangsanfeng'</span>,<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">password</span>: <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'12121121sd'</span><br>}) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//您的权限比较低,目前不能查看</span><br><br><br><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">4.2 接口 只读属性</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">只读属性: 意味着给属性赋值了后,不可改变。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">格式: <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">readonly 属性 : 类型</code></p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">interface Menus {<br> readonly title?:string,<br> icon?:string,<br> readonly path?:string,<br> readonly Layout?:string<br>}<br><br><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getMenuInfo</span>(<span class="hljs-params" style="line-height: 26px">data:Menus</span>)</span>{<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(data)<br> data.icon = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'修改图标'</span> <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 可以修改</span><br> <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// data.path = '/home' 报错,禁止修改,接口属性为只读</span><br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(data)<br>}<br><br><br>getMenuInfo({<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">title</span>: <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'主页'</span>,<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">icon</span>:<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'homes'</span>,<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">path</span>:<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'/home'</span>,<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">Layout</span>: <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'Layput'</span><br>})<br><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">4.3 接口函数类型</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">用来约束函数传递参数类型</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">函数类型的类型检查来说,函数的参数名不需要与接口里定义的名字相匹配。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">格式: <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">(参数1: 类型,参数2:类型) : 返回值类型</code></section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 获取用户信息</span><br>interface UserInfo {<br> (name: string,<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">adress</span>: string,<span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">phone</span>: number) : string<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> getUsefInfo:UserInfo = <span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>(<span class="hljs-params" style="line-height: 26px">name,adress,phone</span>)</span>{<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">return</span> <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${name}</span>住在<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${adress}</span>,手机号为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${phone}</span>`</span><br>}<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(getUsefInfo(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'张锋'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'天津南开区xx小区'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">188888888</span>))<br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">4.4 接口可索引类型</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">在定义一个数组时,可以定义一个 索引类型接口,这样就约束了它必须传递哪些类型的值。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">访问: <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">通过 变量[索引]</code></p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">interface Code{<br> : string<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> errCode : Code = [<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'200 成功'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'301 重定向'</span>, <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'400 客户端错误'</span>, <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'500 服务端出错'</span>]<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(errCode[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">3</span>]) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//500 服务端出错</span><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">4.5 类型接口实现</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">接口描述了类的公共部分,而不是公共和私有两部分。 它不会帮你检查类是否具有某些私有成员。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">interface Animals {<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">eye</span>: number,<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">leg</span>: number, <br>}<br><br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Dog</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Animals</span> </span>{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">eye</span>: number;<br> leg: number;<br> kind: string<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(eye: number, leg: number, kind: string) {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.eye = eye<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.leg = leg<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.kind = kind<br> }<br> getDogInfo(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`品种为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.kind}</span>,有<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.eye}</span>只眼,<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.leg}</span>条腿`</span>)<br> }<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> hashiqi = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> Dog(<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">4</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'哈士奇'</span>);<br>hashiqi.getDogInfo() <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//品种为哈士奇,有2只眼,4条腿</span><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">4.6 接口继承(多合一)</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">接口之间可以互相继承,这样可以更灵活地将接口分割到可重用的模块里。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">interface Shape1 {<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">data</span>: string<br>}<br><br>interface Shape2 extends Shape1{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">code</span>: number<br> <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// Shape2 具有 Shape1 的特征</span><br>}<br><br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Message</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Shape2</span> </span>{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">code</span>: number;<br> data: string;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(code : number,data: string) {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.code = code;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.data = data<br> }<br> getCodeInfo(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`状态码为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.code}</span>,返回信息为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.data}</span>`</span>)<br> }<br>}<br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> err = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> Message(<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">500</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'服务端错误'</span>)<br>err.getCodeInfo() <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//状态码为500,返回信息为服务端错误</span><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">4.7 接口继承类</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">当接口继承了一个类,那么接口也会拥有类的属性和方法。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">当别的类实现这个 接口时,会同时实现 接口的属性和方法, 继承类的属性和方法</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Log</span> </span>{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">time</span>: string = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'2020-11-2'</span>;<br> getLog(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span>)<br> }<br>}<br><br>interface Shape3 extends Log{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">message</span> : string<br>}<br><br><br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">ErrLog</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Shape3</span> </span>{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">message</span>: string ;<br> time: string;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(message: string, time: string) {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.message = message;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.time = time<br> }<br> getLog(): <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span> {<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"Method not implemented."</span>);<br> }<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> errs = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> ErrLog(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'2020-11-2'</span>)<br>errs.getLog() <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//Method not implemented.</span><br><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">五,泛型</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">接触过JAVA 的同学,应该对这个不陌生,非常熟了。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">作为前端的我们,可能第一 次听这个概念。通过 字面意思可以看出,它指代的类型比较广泛。</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">作用:: 避免重复代码,代码冗余</section></li></ul>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">但是它和 any类型 还是有区别的。</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">any类型:如果一个函数类型为any,那么它的参数可以是任意类型,一般传入的类型与返回的类型应该是相同的。如果传入了一个 string 类型的参数,那么我们也不知道它返回啥类型。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">泛型 : 它可以使<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">返回类型 和 传入类型</code> 保持一致,这样我们可以清楚的知道函数返回的类型为什么类型。</section></li></ul>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">5.1 泛型接口</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">泛型接口可以这样理解:</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">当你需要给接口指定类型时,但目前不知道属性类型为什么时,就可以采用泛型接口</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">你可以给接口指定参数为多个泛型类型,也可以单个;当使用时,明确参数类型即可。</p>
</blockquote>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ecd9531f6b6942c8bce86ff798ea8894~tplv-k3u1fbpfcp-zoom-1.image"><figcaption style="text-align: center; opacity: 0.6; font-size: 12px; display: flex; justify-content: center; align-items: center; width: 100%; margin-top: -35px; background: rgba(0, 0, 0, 0.7); color: rgba(255, 255, 255, 1); line-height: 35px; z-index: 20">reJsYR.png</figcaption></figure>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"> interface User <T,S,Y> {<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>: T;<br> hobby: S;<br> age: Y;<br> }<br><br> <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">People</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">User</span><<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">String</span>,<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">String</span>,<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Number</span>> </span>{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>: <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">String</span>;<br> hobby: <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">String</span>;<br> age: <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">Number</span>;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string,hobby:string,age:number){<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name = name;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.hobby = hobby;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age = age; <br> }<br> getInfo(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name+<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"------------------"</span>+<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.hobby)<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name}</span>的年龄为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age}</span>`</span>)<br> } <br> }<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> xiaoZhou = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> People(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小周'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'敲代码'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">22</span>)<br> xiaoZhou.getInfo() <br> <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//小周------------------敲代码</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 小周的年龄为22</span><br><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">5.2 泛型函数</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">定义泛型函数,可以让 传入参数类型参数 和 返回值类型保持一致。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">泛型 标志一般用字母大写,T 可以随意更换</p>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">格式 : 函数名<T> (参数1:T) : 返回值类型 T<br></code></pre>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">genericity</span><<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">T</span>> (<span class="hljs-params" style="line-height: 26px">data: T</span>) : <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">T</span> </span>{<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(data)<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">return</span> data<br>}<br><br>genericity(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"测试"</span>)<br>genericity(<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">666</span>)<br>genericity([<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'前端'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'后端'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'云端'</span>])<br><br><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">5.3 泛型类</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<ol style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">什么是泛型类</section></li></ol>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">它规定了类中属性和方法的 类型,而且必须和类型定义的类型保持一致。</p>
<ol start="2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">泛型类的作用</section></li></ol>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">可以帮助我们确认类的所有属性都在使用相同的类型</p>
<ol start="3" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">使用格式</section></li></ol>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> 类名<<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">T</span>> </span>{<br> name!: T;<br> hobby!: T;<br>}<br><br># 这样这个类的所有类型为 number<br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> 实例 = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> 类名<number>();<br><br></code></pre>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> GenericityA<X>{<br> sex!: X;<br> age!: X;<br>}<br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> gen = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> GenericityA<<span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">number</span>>();<br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// gen.sex = '测试' 报错</span><br>gen.age = <span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">3</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(gen.age)<br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">5.4 泛型约束</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<ol style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">接口约束</section></li></ol>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">通过定义接口, 泛型函数继承接口,则参数必须实现接口中的属性,这样就达到了泛型函数的约束</section></li></ul>
<ol start="2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">类约束</section></li></ol>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">通过给类的泛型指定为另一个类,这样就规定了类泛型的类型都为另一个类</section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"># 第一种<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 定义接口</span><br> interface DataInfo{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">title</span>: string,<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">price</span>: number<br> }<br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 泛型函数 继承接口,进行对参数类型约束, 如果传入的参数中,没有包含接口属性,则编译不通过</span><br> <span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getDataInfos</span>< <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">T</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">extends</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">DataInfo</span>> (<span class="hljs-params" style="line-height: 26px">obj: T</span>) : <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">T</span> </span>{<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">return</span> obj<br> }<br><br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> book = {<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">title</span>: <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'前端进阶'</span>,<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">price</span>: <span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">50</span>, <br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">author</span>: <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小新'</span><br> }<br><br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(getDataInfos(book)) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//{ title: '前端进阶', price: 50, author: '小新' }</span><br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"># 第二种<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 通过类来约束</span><br> <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Login</span></span>{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">username</span>: string;<br> password: string;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(username: string,password:string){<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.username = username<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.password = password<br> }<br> }<br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Mysql</span><<span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">T</span>></span>{<br> login<T>(info:T):T{<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">return</span> info<br> }<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> x = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> Login(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'admin'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'12345'</span>);<br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> mysql = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> Mysql<Login>();<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(mysql.login(x)) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//Login { username: 'admin', password: '12345' }</span><br></code></pre>
<hr data-tool="mdnice编辑器" style="height: 1px; margin: 10px 0; border-top: 1px solid rgba(19, 92, 224, 1); border-right: none; border-bottom: none; border-left: none">
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">六,类 Class</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">说到类,做后端的朋友应该都了解,前端 在ES6 中,才出现了 类 Class 这个关键词。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">Class 有哪些特征</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">属性</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">构造器</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">方法</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">继承<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">extends</code></section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">属性 / 方法 修饰符</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">静态属性</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">抽象类</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">存取器 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">getters/setters</code></section></li></ul>
</blockquote>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">6.1 修饰符</span><span class="suffix" style="display: none"></span></h4>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content"><code>public</code> 共有的</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">当属性 / 方法 修饰符为 public 时, 如果前面没有,默认会加上,我们可以自由的访问程序里定义的成员。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Fruit</span> </span>{<br> public name: string;<br> price: number;<br> <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 以上为等价</span><br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name: string, price: number) {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name = name;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.price = price<br> }<br> getFruitInfo(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`您要购买的水果为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${name}</span>,价格为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.price}</span>`</span>)<br> }<br>}<br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content"><code>private</code> 私有的</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">当成员被标记成 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">private</code>时,它就不能在声明它的类的外部访问。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Fruit</span> </span>{<br> public name: string;<br> private price: number;<br><br> <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 以上为等价</span><br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name: string, price: number) {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name = name;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.price = price<br> }<br> getFruitInfo(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`您要购买的水果为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${name}</span>,价格为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.price}</span>`</span>)<br> }<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> apple = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> Fruit(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'苹果'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">22</span>)<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// console.log(apple.price) 报错, 实例不可以访问私有属性</span><br><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content"><code>protected</code>受保护的</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2"><code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">protected</code>修饰符与 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">private</code>修饰符的行为很相似,但有一点不同, <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">protected</code>成员在派生类中仍然可以访问,不可以通过实例来访问受保护的属性。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">A</span> </span>{<br> protected name : string;<br> protected age : number;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name: string , age: number) {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name = name;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age = age<br> }<br> getA(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'A'</span>)<br> }<br>}<br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">B</span> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">extends</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">A</span> </span>{<br> protected job : string;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name: string, job: string,age: number) {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">super</span>(name,age)<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.job = job<br> }<br> getB(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`B 姓名为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name}</span> && 年龄为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age}</span> && 职业为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.job}</span>,`</span>)<br> }<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> b = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> B(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小飞'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'前端工程师'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">22</span>)<br>b.getA() <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//A</span><br>b.getB() <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//B 姓名为小飞 && 年龄为22 && 职业为前端工程师,</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// console.log(b.name) 报错,访问不了,protected成员只能在派生类中可以访问,不能通过实例来访问。</span><br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">6.2 静态属性</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">类的静态成员(属性 和 方法) 只能通过 类来可以访问。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">定义: static属性 / static 方法</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Food</span> </span>{<br> public name: string;<br> private price: number;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">static</span> adress: string = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'四川'</span>;<br> <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 以上为等价</span><br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name: string, price: number) {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name = name;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.price = price<br> }<br> getFruitInfo(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`您要购买的东西为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${name}</span>,价格为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.price}</span>`</span>)<br> }<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> spicy = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> Food(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'辣条'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">3</span>)<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(Food.adress) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//四川</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// console.log(spicy.adress) 报错 类的实例对象不可以访问 类的静态属性。 只可以通过类.属性来访问</span><br><br><br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">6.3 继承 <code>extend</code></span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">继承的本意很好理解,当子类继承了父类,那么子类就拥有了父类的特征(属性) 和 行为(方法),</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">T</span> </span>{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:string;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string){<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name = name<br> }<br> getNames(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'继承类T'</span>)<br> }<br>}<br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">S</span> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">extends</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">T</span> </span>{<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string){<br> <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 派生类拥有T属性和方法</span><br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">super</span>(name)<br> }<br> getName(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name)<br> }<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> ss = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> S(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试继承'</span>)<br>ss.getName() <br>ss.getNames()<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 测试继承</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 继承类T</span><br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">6.4 抽象类</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">抽象类可以包含成员的实现细节。 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">abstract</code>关键字是用于定义抽象类和在抽象类内部定义抽象方法。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">抽象类中的抽象方法不包含具体实现并且必须在派生类中实现。</section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">abstract <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">E</span></span>{<br> abstract name: string;<br> abstract speak():<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span>;<br> abstract play():<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span>;<br>}<br><br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">F</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">E</span> </span>{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>: string;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string){<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name = name<br> }<br> <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 派生类 F 必须实现 抽象类E 的方法和属性 </span><br> speak(): <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span> {<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'具有聊天功能'</span>)<br> }<br> play(): <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span> {<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'具有娱乐功能'</span>)<br> }<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">get</span>(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name)<br> }<br>}<br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> f = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> F(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span>);<br>f.play() <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//具有娱乐功能</span><br>f.get() <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 测试</span><br>f.speak() <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//具有聊天功能</span><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">七,TS 中的函数</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">函数类型包括 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">参数类型 和 返回值类型</code></p>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">7.1 函数添加返回值类型</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">每个参数添加类型之后再为函数本身添加返回值类型.</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">TypeScript能够根据返回语句自动推断出返回值类型,因此我们通常省略它。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">下面会介绍在TS 中,两种写函数的格式</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 第一种写法</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> getInterFaceInfo : <span class="hljs-function" style="line-height: 26px">(<span class="hljs-params" style="line-height: 26px">obj:object</span>) =></span> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span> = <span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>(<span class="hljs-params" style="line-height: 26px">obj</span>)</span>{<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(obj)<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> infos: object = {<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">code</span>: <span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">200</span>,<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">message</span>: <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'发送成功'</span><br>}<br>getInterFaceInfo(infos)<br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 第二种写法</span><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getCode</span>(<span class="hljs-params" style="line-height: 26px">code: number, message:string</span>) : <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">void</span> </span>{<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`code为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${code}</span>,message为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${message}</span>`</span>)<br>}<br><br>getCode(<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">200</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'接受成功'</span>) <br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">7.2 函数可选参数/默认参数</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">JavaScript里,每个参数都是可选的,可传可不传。 没传参的时候,它的值就是undefined。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">在TypeScript里我们可以在参数名旁使用 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">?</code>实现可选参数的功能。</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2"><strong style="background: linear-gradient(90deg, rgba(50, 153, 210, 1), rgba(239, 189, 181, 1)); color: rgba(255, 255, 255, 1); font-weight: 400; padding: 0 4px; display: inline-block; border-radius: 4px; margin: 0 2px; letter-spacing: 1px">可选参数必须放在必须参数后面。</strong></section></li></ul>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">格式 : 函数名(变量名?:类型):类型 {} <br></code></pre>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">默认参数,在传递参数时,指定默认值</section></li></ul>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">格式 : 函数名(变量名 :类型 = "xx"):类型 {}
</code></pre>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 可选参数</span><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getNetWork</span>(<span class="hljs-params" style="line-height: 26px">ip:string,domain:string,agreement?:string</span>)</span>{<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`ip地址为:<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${ip}</span>,域名为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${domain}</span>,协议为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${agreement}</span>`</span>)<br>}<br><br>getNetWork(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'127.0.0.1'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'www.xiaomi.com'</span>) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//ip地址为:127.0.0.1,域名为www.xiaomi.com,协议为undefined</span><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 默认参数</span><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getNetWorks</span>(<span class="hljs-params" style="line-height: 26px">ip:string,domain:string,agreement:string = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'http'</span></span>)</span>{<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`ip地址为:<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${ip}</span>,域名为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${domain}</span>,协议为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${agreement}</span>`</span>)<br>}<br>getNetWorks(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'127.0.0.1'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'www.xiaomi.com'</span>) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//ip地址为:127.0.0.1,域名为www.xiaomi.com,协议为http </span><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">7.3 函数剩余参数</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">有时,你想同时操作多个参数,或者你并不知道会有多少参数传递进来。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">在JavaScript里,你可以使用 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">arguments</code>来访问所有传入的参数。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">在TypeScript 中,可以把所有参数集中在一个变量中,前面加上<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">...</code> 表示 剩余参数。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">注意</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">直接通过变量访问</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">也可以通过索引访问</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">只能定义一个剩余参数,且位置在 默认参数和可选参数后面</section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getNumberInfo</span>(<span class="hljs-params" style="line-height: 26px">num:number,...peopleArray: string []</span>) </span>{<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`人员个数为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${num}</span>,成员为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${peopleArray}</span>`</span>) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 也可以通过索引来获取元素</span><br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`人员个数为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${num}</span>,成员为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${peopleArray[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1</span>]}</span>`</span>) <br>}<br><br>getNumberInfo(<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">4</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小明'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小李'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小红'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小张'</span>) <br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//人员个数为4,成员为小明,小李,小红,小张</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//人员个数为4,成员为小李</span><br></code></pre>
<hr data-tool="mdnice编辑器" style="height: 1px; margin: 10px 0; border-top: 1px solid rgba(19, 92, 224, 1); border-right: none; border-bottom: none; border-left: none">
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">八,枚举</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">枚举可以清晰地表达一组对应关系。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">TypeScript支持数字的和基于字符串的枚举。</p>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">8.1 数字枚举</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">默认枚举的顺序以 0 开头,然后自动递增。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">枚举顺序也可以指定 值, 指定后,它前面第一个还是以0 递增</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">访问</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">通过 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">枚举名.属性</code>访问到的是 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">序号</code></section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">通过 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">枚举名[序号]</code>访问到的是 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">属性名</code></section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">enum Sex {<br> x,<br> man = <span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">4</span>,<br> woman <br>}<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(Sex.x) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//0</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`小红的性别为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${Sex[<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">5</span>]}</span>`</span>) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//小红的性别为woman</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`后端接受小红的性别ID <span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${Sex.woman}</span>`</span>) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//后端接受小红的性别ID 5</span><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">8.2 字符串枚举</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">enum Job {<br> frontEnd = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'前端'</span>,<br> backEnd = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'后端'</span><br>}<br><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(Job) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//{ frontEnd: '前端', backEnd: '后端' } </span><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">九,高级类型</span><span class="suffix"></span></h2>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">9.1 交叉类型</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">它指 可以将多个类型合并为一个类型。标识符为 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">&</code> , 当指定一个变量类型为 交叉类型时,那么它拥有交叉类型的所有属性,也就是并集。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">interface DonInterface {<br> run():<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span>;<br>}<br>interface CatInterface {<br> jump():<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span>;<br>}<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//这里的pet将两个类型合并,所以pet必须保护两个类型所定义的方法</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> pet : DonInterface & CatInterface = {<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">run</span>:<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>(<span class="hljs-params" style="line-height: 26px"></span>)</span>{},<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">jump</span>:<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>(<span class="hljs-params" style="line-height: 26px"></span>)</span>{}<br>}<br><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">9.2 联合类型</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">联合类型表示一个值可以是几种类型之一。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">用竖线( <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">|</code>)分隔每个类型。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">一个值是联合类型,我们只能访问此联合类型的所有类型里共有的成员。</section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"> <span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getMenus</span>(<span class="hljs-params" style="line-height: 26px">info: string | number</span>) </span>{<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(info)<br> }<br><br><br>getMenus(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"测试"</span>)<br>getMenus(<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2</span>)<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// getMenus(false) 报错</span><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">十,模块</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">模块: 定义的变量,函数,类等等,只能在自身的作用域里使用。 如果想在外部访问使用,那么必须使用<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">export</code> 将其导出即可。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">使用模块: 通过 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">import</code>将模块内容导入即可使用。</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">模块是自声明的;两个模块之间的关系是通过在文件级别上使用imports和exports建立的。</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">模块使用模块加载器去导入其它的模块。 在运行时,模块加载器的作用是在执行此模块代码前去查找并执行这个模块的所有依赖。</section></li></ul>
</blockquote>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">10.导出</span><span class="suffix" style="display: none"></span></h4>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">10.1 导出声明</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">任何声明(比如变量,函数,类,类型别名或接口)都能够通过添加<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">export</code>关键字来导出。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2"><code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">导出可以对任何声明 进行重命名,防止命名冲突, 通过 as 来修改</code></p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"># 模块A 文件<br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 导出接口</span><br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> interface A {<br> getList() : <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span><br> }<br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 导出变量</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> GET_METHOD = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"get"</span><br> <br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 导出类</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">S</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">A</span> </span>{<br> getList(): <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span> {<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"导出类"</span>)<br> }<br> <br>}<br><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getQueryData</span>(<span class="hljs-params" style="line-height: 26px"></span>):<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">void</span> </span>{<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"获取分页数据"</span>)<br>}<br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 导出模块 变量重命名</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> { getQueryData <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">as</span> getQuery}<br><br><br><br># 文件B<br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">import</span> {getQuery,S,A} <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'./模块A'</span>;<br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 使用模块中的函数</span><br>getQuery()<br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 实例模块中类的对象</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> a = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> S();<br>a.getList() <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 输出导出类</span><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 实现模块中的 A 接口</span><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Y</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">A</span> </span>{<br> getList(): <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">void</span> {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">throw</span> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">Error</span>(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'Method not implemented.'</span>);<br> }<br> <br>}<br><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">10.2 组合模块使用</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">通常一个大的模块是多个子模块组成的。那么我们可以通过 在大的模块中导入多个子模块。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">格式: <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">export * from "模块"</code></p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">使用组合模块: <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">import * as 重命名变量 from ‘组合模块路径’</code></p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"># 模块C<br> <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 导出变量</span><br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> GET_METHOD = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"get"</span><br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"># 模块B<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> str: string = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"B模块"</span><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> <span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getContent</span>(<span class="hljs-params" style="line-height: 26px"></span>):<span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">void</span></span>{<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"我是模块B的内容"</span>)<br>}<br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#组合模块<br><br><br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> res : object = {<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">code</span>: <span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">200</span>,<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">message</span>: <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"请求成功"</span><br> }<br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> <span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getRes</span>(<span class="hljs-params" style="line-height: 26px"></span>): <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">void</span> </span>{<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(res)<br> }<br><br> <br> # 导出子模块<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> * <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"./modulesC"</span><br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> * <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"./moduleB"</span><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">10.3 使用组合模块</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">import</span> * <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">as</span> T <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"./modulesA"</span>;<br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// C 模块中的</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(T.GET_METHOD)<br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// B 模块中的内容</span><br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(T.str) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//B模块</span><br>T.getContent() <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//我是模块B的内容</span><br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// A 模块中的内容</span><br>T.getRes() <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//{ code: 200, message: '请求成功' } </span><br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">10.4 默认导出</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">每个模块都可以有一个<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">default</code>导出。 默认导出使用 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">default</code>关键字标记;并且一个模块只能够有一个<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">default</code>导出。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#模块<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> interface K {<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:string;<br> birth:string;<br>}<br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">default</span> <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Student</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">K</span> </span>{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>: string;<br> birth: string;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string,birth:string){<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name = name;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.birth = birth;<br> } <br> getStudentInfo(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name+<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.birth)<br> }<br>}<br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#文件A<br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">import</span> D,{K} <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">from</span> <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'./modulesD'</span><br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 使用默认导出</span><br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> d = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> D(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小明'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'1998'</span>)<br> d.getStudentInfo()<br><br><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 参数类型为接口K </span><br> <span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">getMessage</span>(<span class="hljs-params" style="line-height: 26px">obj: K</span>): <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">void</span> </span>{<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(obj)<br> }<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> obj = {<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"小红"</span>,<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">birth</span>: <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"1998"</span><br> }<br> getMessage(obj);<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400"></p>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">10.5 <code>export = 和 import = require()</code></span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">CommonJS和AMD的环境里都有一个<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">exports</code>变量,这个变量包含了一个模块的所有导出内容。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">CommonJS和AMD的<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">exports</code>都可以被赋值为一个<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">对象</code></p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2"><code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">exports 和 export default</code> 用途一样,但是 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">export default</code> 语法不能兼容CommonJS和AMD的<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">exports</code>。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">在TypeScript 中,为了达到这样效果,可以这样写:</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">导出: <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">export =</code> 等于 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">exports</code></p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">导入: <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">import module = require("module")</code></p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"># 模块<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 相当于默认导出</span><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> = <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Mongodbs</span></span>{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">host</span>:string;<br> user:string;<br> password:string;<br> port:number;<br> databaseName:string;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(host:string,user:string,password:string,port:number,databaseName:string) {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.host = host;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.user = user;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.password = password;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.port = port;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.databaseName = databaseName<br> }<br> query(table:string){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`select * from <span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${table}</span>`</span>)<br> }<br>}<br><br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">#使用模块<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">import</span> MogoDb = <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">require</span>(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"./modulesE"</span>) <br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> mogodb = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> MogoDb(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'1270.0.1'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'admin'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'123456'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">3006</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'TypeScript'</span>)<br><br>mogodb.query(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'Vue'</span>) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//select * from Vue</span><br><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">十一, 命名空间</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<ol style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">定义</section></li></ol>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">“内部模块”称为“命名空间”</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">“外部模块”称为“模块”</section></li></ul>
<ol start="2" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">作用</section></li></ol>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">减少命名冲突,将代码组织到一个空间内,便于访问。</section></li></ul>
<ol start="3" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">使用格式</section></li></ol>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">通过 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">namespace 空间名 { }</code>,内部通过<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">export</code> 导出来使用内部成员</section></li></ul>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">namespace XiaoXin {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> interface GetData{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>: string;<br> price: number;<br> getInfo(obj:object):any;<br> }<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> interface GetMessage {<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">code</span>: number;<br> message: string;<br> }<br><br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Book</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">implements</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">GetData</span></span>{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>: string;<br> price: number;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string,price:number){<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name = name;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.price = price<br> }<br> getInfo(obj: object) {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">throw</span> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">Error</span>(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"Method not implemented."</span>);<br> }<br> buyBook(obj: GetMessage) {<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(obj)<br> }<br> } <br>}<br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> fontEnd = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> XiaoXin.Book(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"前端开发手册"</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">99</span>)<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">var</span> obj = {<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">code</span>: <span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">200</span>,<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">message</span>:<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"购买成功"</span><br>}<br><br>fontEnd.buyBook(obj) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//{ code: 200, message: '购买成功' }</span><br><br><br><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">test</span>(<span class="hljs-params" style="line-height: 26px">obj:XiaoXin.GetMessage</span>)</span>{<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(obj)<br>}<br><br>test(obj) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//{ code: 200, message: '购买成功' } </span><br></code></pre>
<h4 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">11.1 拆分命名空间</span><span class="suffix" style="display: none"></span></h4>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">当应用变得越来越大时,我们需要将代码分离到不同的文件中以便于维护。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">我们可以将命名空间文件拆分成多个文件,但是它们的命名空间名还是使用的同一个,各个文件相互依赖使用。但是必须文件最开头引入 命名空间文件。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">格式:<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">/// <reference path="MS1.ts"/></code></p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"># 根命名空间<br>namespace School {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> schoolName = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">"清华大学"</span><br>}<br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"># 子命名空间<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">/// <reference path="MS1.ts" /></span><br><br>namespace School{<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Teacher</span> </span>{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">faculty</span>:string;<br> name:string;<br> age:number;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(faculty:string,name:string,age:number){<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.faculty = faculty;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name = name;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age = age<br> }<br> getInfo(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name}</span>为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.faculty}</span>,年龄为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age}</span>`</span>)<br> }<br> getSchool(schoole:string){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name}</span>老师就职于<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${schoole}</span>`</span>)<br> }<br> }<br>}<br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"># 子命名空间<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">/// <reference path="MS1.ts" /></span><br><br> namespace School{<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">export</span> <span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Student</span></span>{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:string;<br> age:number;<br> hobby:string;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string,age:number,hobby:string) {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name = name;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age = age;<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.hobby = hobby;<br> }<br> getInfo(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name}</span>是一个学生,年龄为<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.age}</span>,爱好是<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.hobby}</span>`</span>)<br> }<br> }<br> }<br><br></code></pre>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"># 使用合并的命名空间<br><br><br>导入命名空间<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">/// <reference path="MS1.ts" /></span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">/// <reference path="MS2.ts" /></span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">/// <reference path="MS4.ts" /></span><br><br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> teacher = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> School.Teacher(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'计算机教授'</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'张博士'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">34</span>);<br>teacher.getInfo() <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//张博士为计算机教授,年龄为34</span><br>teacher.getSchool(School.schoolName) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//张博士老师就职于清华大学</span><br><br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> students = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> School.Student(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'张三'</span>,<span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">17</span>,<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'玩LOL'</span>);<br>students.getInfo() <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//张三是一个学生,年龄为17,爱好是玩LOL</span><br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400"></p>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">编译命名空间文件</span><span class="suffix" style="display: none"></span></h5>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">第一种方法: 会编译为 一个js文件<br>tsc --outFile sample.js Test.ts<br><br><br>第二种方法: 会编译为多个js文件,然后通过 <script> src 引入js文件即可<br>tsc --outFile sample.js Validation.ts LettersOnlyValidator.ts ZipCodeValidator.ts Test.ts<br><br></code></pre>
<h2 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); position: relative; font-size: 20px"><span class="prefix" style="display: none"></span><span class="content" style="border-left: 4px solid; padding-left: 10px">十二,装饰器</span><span class="suffix"></span></h2>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">装饰器是一种特殊类型的声明,它能够附加到类声明、方法、访问符、属性、类方法的参数上,以达到扩展类的行为。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">自从 ES2015 引入 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">class</code>,当我们需要在多个不同的类之间共享或者扩展一些方法或行为的时候,代码会变得错综复杂,极其不优雅,这也是装饰器被提出的一个很重要的原因。</p>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">12.1 修饰器分类</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">类装饰器</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">属性装饰器</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">方法装饰器</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2">参数装饰器</section></li></ul>
<pre class="custom" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px">修饰器写法: <span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">1.</span> 普通修饰器 (不传参数)<br> <span class="hljs-number" style="color: rgba(152, 104, 1, 1); line-height: 26px">2.</span> 装饰器工厂 (传参数)<br></code></pre>
</blockquote>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">12.2 类装饰器</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">类装饰器表达式会在运行时当作函数被调用,类的构造函数作为其唯一的参数。</p>
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">使用场景:应用于类构造函数,可以用来监视,修改或替换类定义。</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">const</span> extension = (<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>: Function):any => {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>.prototype.coreHour = '10:00-15:00'<br> <br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>.prototype.meeting = () => {<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'重载:Daily meeting!'</span>);<br> }<br><br> }<br><br>@extension<br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Employee</span> </span>{<br> public name!: string<br> public department!: string<br><br><br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name: string, department: string) {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name = name<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.department = department<br> }<br><br> meeting() {<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'Every Monday!'</span>)<br> }<br><br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> e: any = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> Employee(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'装饰器'</span>, <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'测试'</span>)<br><span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(e) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">//Employee { name: '装饰器', department: '测试' }</span><br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(e.coreHour) <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 10:00-15:00</span><br> e.meeting() <span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 重载:Daily meeting!</span><br> <br></code></pre>
<h5 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; font-size: 16px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">12.3 类属性装饰器</span><span class="suffix" style="display: none"></span></h5>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">作用于类属性的装饰器表达式会在运行时当作函数被调用,传入下列3个参数 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">target</code>、<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">name</code>、<code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">descriptor</code>:</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc">
<li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">target</code>: 对于静态成员来说是类的构造函数,对于实例成员是类的原型对象</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">name</code>: 成员的名字</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500; line-height: 2"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(30, 107, 184, 1); background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all">descriptor</code>: 成员的属性描述符</section></li></ul>
<h4 style="margin: 0; font-weight: bold; font-size: 18px; padding: 30px 0; color: rgba(19, 92, 224, 1)"><span class="prefix" style="display: none"></span><span class="content">执行顺序: 当调用有装饰器的函数时,会先执行装饰器,后再执行函数。</span><span class="suffix" style="display: none"></span></h4>
</blockquote>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(178, 174, 197, 1); display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; color: rgba(106, 115, 125, 1); padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; background: rgba(255, 249, 249, 1)">
<p style="padding-top: 8px; padding-bottom: 8px; font-size: 16px; font-weight: 400; margin: 0; color: rgba(102, 102, 102, 1); line-height: 2">通过修饰器完成一个属性只读功能,其实就是修改数据描述符中的 <code style="font-size: 14px; word-wrap: break-word; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; color: rgba(19, 148, 216, 1); padding: 2px 6px; word-break: normal">writable</code> 的值 :</p>
</blockquote>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.55)"><span style="display: block; background: url("https://files.mdnice.com/user/3441/876cad08-0422-409d-bb5a-08afec5da8ee.svg") 10px 10px / 40px no-repeat rgba(250, 250, 250, 1); height: 30px; width: 100%; margin-bottom: -7px; border-radius: 5px"></span><code class="hljs" style="overflow-x: auto; padding: 15px 16px 16px; color: rgba(56, 58, 66, 1); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; background: rgba(250, 250, 250, 1); border-radius: 5px"><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span> <span class="hljs-title" style="color: rgba(64, 120, 242, 1); line-height: 26px">readonly</span>(<span class="hljs-params" style="line-height: 26px">value: boolean</span>)</span>{<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">return</span> <span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">function</span>(<span class="hljs-params" style="line-height: 26px">target:any,name:string,descriptor:PropertyDescriptor</span>) </span>{<br> descriptor.writable = value<br> }<br>}<br><br><br><span class="hljs-class" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">class</span> <span class="hljs-title" style="color: rgba(193, 132, 1, 1); line-height: 26px">Student</span></span>{<br> <span class="hljs-attr" style="color: rgba(152, 104, 1, 1); line-height: 26px">name</span>:string;<br> school:string = <span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'社会大学'</span><br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">constructor</span>(name:string) {<br> <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name = name<br> }<br> @readonly(<span class="hljs-literal" style="color: rgba(1, 132, 187, 1); line-height: 26px">false</span>)<br> getDataInfo(){<br> <span class="hljs-built_in" style="color: rgba(193, 132, 1, 1); line-height: 26px">console</span>.log(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">`<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.name}</span>毕业于<span class="hljs-subst" style="color: rgba(228, 86, 73, 1); line-height: 26px">${<span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">this</span>.school}</span>`</span>)<br> }<br>}<br><br><span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">let</span> sss = <span class="hljs-keyword" style="color: rgba(166, 38, 164, 1); line-height: 26px">new</span> Student(<span class="hljs-string" style="color: rgba(80, 161, 79, 1); line-height: 26px">'小李子'</span>)<br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// 报错, 只能读,不能修改</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// sss.getDataInfo = () => {</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// console.log("测试修改")</span><br><span class="hljs-comment" style="color: rgba(160, 161, 167, 1); font-style: italic; line-height: 26px">// }</span><br><br>sss.getDataInfo()<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400">更多精彩文章在主页</p>
<h3 data-tool="mdnice编辑器" style="margin: 0; font-weight: bold; padding: 30px 0; color: rgba(19, 92, 224, 1); font-size: 16px"><span class="prefix" style="display: none"></span><span class="content">最后</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgba(0, 0, 0, 1); font-size: 16px; line-height: 2; font-weight: 400">文中如有错误,欢迎码友在评论区指正,如果对你有所帮助,欢迎点赞和关注~~~
<img src="https://img-hello-world.oss-cn-beijing.aliyuncs.com/imgs/497ff10ccd161003e02b5b4bbdd8548e.png">
</p>
</section><br><br>
来源:https://www.cnblogs.com/HaiJun-Aion/p/15193108.html
頁:
[1]