琦宝儿 發表於 2025-3-17 23:20:00

JavaScript 简写神技

<p style="margin-bottom: 1rem; color: rgba(51, 51, 51, 1); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; font-size: 16px; letter-spacing: normal; text-align: start" data-line="2">JavaScript 是一门强大且灵活的语言,拥有丰富的特性和语法糖。分享下 16 个最常用的 JavaScript 的简写技巧,掌握它们可以让我们编写出更简洁、更优雅的代码,并显著提升开发效率(增加摸鱼时间)。</p>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="4">1. 三元运算符简化条件判断</h3>
<pre><code style="font-family: &quot;Fira Code&quot;, Menlo, Consolas, &quot;Source Code Pro&quot;, monospace; font-size: 14px; padding: 0.5em; background: rgba(248, 248, 248, 1); border-radius: 3px; word-break: normal; display: block; overflow-x: auto"><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 传统写法<br><span style="font-weight: bold">let result;<br><span style="font-weight: bold">if (someCondition) {<br>    result = <span style="color: rgba(221, 17, 68, 1)">'yes';<br>} <span style="font-weight: bold">else {<br>    result = <span style="color: rgba(221, 17, 68, 1)">'no';<br>}<br><br><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 简写方式<br><span style="font-weight: bold">const result = someCondition ? <span style="color: rgba(221, 17, 68, 1)">'yes' : <span style="color: rgba(221, 17, 68, 1)">'no';<br></span></span></span></span></span></span></span></span></span></span></code></pre>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="19">2. 空值合并运算符</h3>
<pre><code style="font-family: &quot;Fira Code&quot;, Menlo, Consolas, &quot;Source Code Pro&quot;, monospace; font-size: 14px; padding: 0.5em; background: rgba(248, 248, 248, 1); border-radius: 3px; word-break: normal; display: block; overflow-x: auto"><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 传统写法<br><span style="font-weight: bold">const name = user.name !== <span style="color: rgba(0, 128, 128, 1)">null &amp;&amp; user.name !== <span style="color: rgba(0, 128, 128, 1)">undefined ? user.name : <span style="color: rgba(221, 17, 68, 1)">'default';<br><br><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 简写方式<br><span style="font-weight: bold">const name = user.name ?? <span style="color: rgba(221, 17, 68, 1)">'default';<br></span></span></span></span></span></span></span></span></code></pre>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="29">3. 可选链操作符</h3>
<pre><code style="font-family: &quot;Fira Code&quot;, Menlo, Consolas, &quot;Source Code Pro&quot;, monospace; font-size: 14px; padding: 0.5em; background: rgba(248, 248, 248, 1); border-radius: 3px; word-break: normal; display: block; overflow-x: auto"><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 传统写法<br><span style="font-weight: bold">const street = user &amp;&amp; user.address &amp;&amp; user.address.street;<br><br><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 简写方式<br><span style="font-weight: bold">const street = user?.address?.street;<br></span></span></span></span></code></pre>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="39">4. 数组去重</h3>
<pre><code style="font-family: &quot;Fira Code&quot;, Menlo, Consolas, &quot;Source Code Pro&quot;, monospace; font-size: 14px; padding: 0.5em; background: rgba(248, 248, 248, 1); border-radius: 3px; word-break: normal; display: block; overflow-x: auto"><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 传统写法<br><span style="font-weight: bold">function <span style="color: rgba(153, 0, 0, 1); font-weight: bold">unique(arr) {<br>    <span style="font-weight: bold">return arr.<span style="color: rgba(153, 0, 0, 1); font-weight: bold">filter((item, index) =&gt; arr.<span style="color: rgba(153, 0, 0, 1); font-weight: bold">indexOf(item) === index);<br>}<br><br><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 简写方式<br><span style="font-weight: bold">const <span style="color: rgba(153, 0, 0, 1); font-weight: bold">unique = arr =&gt; [...<span style="font-weight: bold">new <span style="color: rgba(153, 0, 0, 1); font-weight: bold">Set(arr)];<br></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="51">5. 快速取整</h3>
<pre><code style="font-family: &quot;Fira Code&quot;, Menlo, Consolas, &quot;Source Code Pro&quot;, monospace; font-size: 14px; padding: 0.5em; background: rgba(248, 248, 248, 1); border-radius: 3px; word-break: normal; display: block; overflow-x: auto"><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 传统写法<br><span style="font-weight: bold">const floor = <span style="color: rgba(153, 0, 0, 1); font-weight: bold">Math.<span style="color: rgba(153, 0, 0, 1); font-weight: bold">floor(<span style="color: rgba(0, 128, 128, 1)">4.9);<br><br><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 简写方式<br><span style="font-weight: bold">const floor = ~~<span style="color: rgba(0, 128, 128, 1)">4.9;<br></span></span></span></span></span></span></span></span></code></pre>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="61">6. 合并对象</h3>
<pre><code style="font-family: &quot;Fira Code&quot;, Menlo, Consolas, &quot;Source Code Pro&quot;, monospace; font-size: 14px; padding: 0.5em; background: rgba(248, 248, 248, 1); border-radius: 3px; word-break: normal; display: block; overflow-x: auto"><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 传统写法<br><span style="font-weight: bold">const merged = <span style="color: rgba(153, 0, 0, 1); font-weight: bold">Object.<span style="color: rgba(153, 0, 0, 1); font-weight: bold">assign({}, obj1, obj2);<br><br><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 简写方式<br><span style="font-weight: bold">const merged = {...obj1, ...obj2};<br></span></span></span></span></span></span></code></pre>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="71">7. <span>短路求值</span></h3>
<pre><code style="font-family: &quot;Fira Code&quot;, Menlo, Consolas, &quot;Source Code Pro&quot;, monospace; font-size: 14px; padding: 0.5em; background: rgba(248, 248, 248, 1); border-radius: 3px; word-break: normal; display: block; overflow-x: auto"><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 传统写法<br><span style="font-weight: bold">if (condition) {<br>    <span style="color: rgba(153, 0, 0, 1); font-weight: bold">doSomething();<br>}<br><br><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 简写方式<br>condition &amp;&amp; <span style="color: rgba(153, 0, 0, 1); font-weight: bold">doSomething();<br></span></span></span></span></span></code></pre>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="83">8. 默认参数值</h3>
<pre><code style="font-family: &quot;Fira Code&quot;, Menlo, Consolas, &quot;Source Code Pro&quot;, monospace; font-size: 14px; padding: 0.5em; background: rgba(248, 248, 248, 1); border-radius: 3px; word-break: normal; display: block; overflow-x: auto"><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 传统写法<br><span style="font-weight: bold">function <span style="color: rgba(153, 0, 0, 1); font-weight: bold">greet(name) {<br>    name = name || <span style="color: rgba(221, 17, 68, 1)">'Guest';<br>    <span style="color: rgba(0, 128, 128, 1)">console.<span style="color: rgba(153, 0, 0, 1); font-weight: bold">log(<span style="color: rgba(221, 17, 68, 1)">`Hello <span style="color: rgba(51, 51, 51, 1)">${name}`);<br>}<br><br><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 简写方式<br><span style="font-weight: bold">const <span style="color: rgba(153, 0, 0, 1); font-weight: bold">greet = (name = <span style="color: rgba(221, 17, 68, 1)">'Guest') =&gt; <span style="color: rgba(0, 128, 128, 1)">console.<span style="color: rgba(153, 0, 0, 1); font-weight: bold">log(<span style="color: rgba(221, 17, 68, 1)">`Hello <span style="color: rgba(51, 51, 51, 1)">${name}`);<br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="96">9. 解构赋值</h3>
<p><img src="https://images.cnblogs.com/cnblogs_com/lzhdim/2440925/o_250113081832_1.png" width="640" height="212"></p>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="107">10. 字符串转数字</h3>
<p><img src="https://images.cnblogs.com/cnblogs_com/lzhdim/2440925/o_250113081837_2.png" width="640" height="180"></p>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="117">11. 多重条件判断</h3>
<p><img src="https://images.cnblogs.com/cnblogs_com/lzhdim/2440925/o_250113081841_3.png" width="718" height="306"></p>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="131">12. 快速幂运算</h3>
<p><img src="https://images.cnblogs.com/cnblogs_com/lzhdim/2440925/o_250113081846_4.png" width="640" height="180"></p>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="141">13. 对象属性简写</h3>
<p><img src="https://images.cnblogs.com/cnblogs_com/lzhdim/2440925/o_250113081850_5.png" width="640" height="180"></p>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="151">14. 数组映射</h3>
<p><img src="https://images.cnblogs.com/cnblogs_com/lzhdim/2440925/o_250113081854_6.png" width="660" height="274"></p>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="164">15. 交换变量值</h3>
<p><img src="https://images.cnblogs.com/cnblogs_com/lzhdim/2440925/o_250113081859_7.png" width="640" height="244"></p>
<h3 style="margin-top: 1.2em; margin-bottom: 16px; line-height: 1.2; color: rgba(0, 0, 0, 1); font-size: 1.331em; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; letter-spacing: normal; text-align: start" data-line="176">16. 动态对象属性</h3>
<pre><code style="font-family: &quot;Fira Code&quot;, Menlo, Consolas, &quot;Source Code Pro&quot;, monospace; font-size: 14px; padding: 0.5em; background: rgba(248, 248, 248, 1); border-radius: 3px; word-break: normal; display: block; overflow-x: auto"><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 传统写法<br><span style="font-weight: bold">const obj = {};<br>obj = value;<br><br><span style="color: rgba(153, 153, 136, 1); font-style: italic">// 简写方式<br><span style="font-weight: bold">const obj = {<br>    [<span style="color: rgba(221, 17, 68, 1)">`<span style="color: rgba(51, 51, 51, 1)">${dynamic}name`]: value<br>};<br></span></span></span></span></span></span></span></code></pre>
<p style="margin-bottom: 1rem; color: rgba(51, 51, 51, 1); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Segoe UI&quot;, Arial, freesans, sans-serif; font-size: 16px; letter-spacing: normal; text-align: start" data-line="189">欢迎补充。</p>

</div>
<div id="MySignature" role="contentinfo">
    <!--
    博客签名HTML

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    E-Mail: lzhdim@163.com
    Blog:   http://lzhdim.cnblogs.com
    Date:   2022-03-23 18:00:00

    使用方法:
                //在博客里添加该代码
-->

<br><br>
<table cellpadding="0" cellspacing="0" class="field" style="background-color: #EEE; width: 100%">
    <tbody>
    <tr>
      <td align="center" width="110px"><img
                height="100px" src="https://images.cnblogs.com/cnblogs_com/lzhdim/636184/o_230607054137_lzhdim.png"
                width="100px"></td>
      <td align="left">
            <span style="font-size: 10pt; color: #223355">&nbsp;&nbsp;&nbsp; Austin Liu&nbsp; 刘恒辉</span>
            <br><span style="font-size: 10pt; color: #223355">&nbsp;&nbsp;&nbsp; Project Manager and Software Designer</span><br><br>
            <span style="font-size: 10pt; color: #223355">&nbsp;&nbsp;&nbsp; E-Mail:lzhdim@163.com</span><br>
            <span style="font-size: 10pt; color: #223355">&nbsp;&nbsp;&nbsp; Blog:https://lzhdim.cnblogs.com<br></span><br>
            <span style="font-size: 10pt; color: #223355">&nbsp;&nbsp;&nbsp;
                  欢迎收藏和转载此博客中的博文,但是请注明出处,给笔者一个与大家交流的空间。谢谢大家。<br></span>
      </td>
    </tr>
    </tbody>
</table><br><br>
来源:https://www.cnblogs.com/lzhdim/p/18668822
頁: [1]
查看完整版本: JavaScript 简写神技