🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# ES6新语法 const 变量不变 let 变量可变 箭头函数,支持默认设置参数 ``` <pre class="calibre14">``` const fn<span class="token">=</span><span class="token2">(</span>a<span class="token">=</span><span class="token3">1</span><span class="token2">,</span>b<span class="token">=</span><span class="token3">2</span><span class="token2">)</span><span class="token">=</span><span class="token">></span><span class="token2">{</span><span class="token5">return</span> a<span class="token">+</span>b<span class="token2">;</span><span class="token2">}</span> const fn1 <span class="token">=</span> <span class="token2">(</span>a<span class="token2">,</span>b<span class="token2">)</span><span class="token">=</span><span class="token">></span>a<span class="token">+</span>b<span class="token2">;</span> <span class="token6">//简写,当返回值可省略括号</span> ``` ``` 字符模板,不再使用+链接字符串 ``` <pre class="calibre14">``` const a <span class="token">=</span> <span class="token3">20</span><span class="token2">;</span> const b <span class="token">=</span> <span class="token3">30</span><span class="token2">;</span> const string <span class="token">=</span> `$<span class="token2">{</span>a<span class="token2">}</span><span class="token">+</span>$<span class="token2">{</span>b<span class="token2">}</span><span class="token">=</span>$<span class="token2">{</span>a<span class="token">+</span>b<span class="token2">}</span>`<span class="token2">;</span> ``` ``` 解析结构 ``` <pre class="calibre14">``` const obj<span class="token">=</span><span class="token2">{</span> a<span class="token2">:</span><span class="token3">1</span><span class="token2">,</span> b<span class="token2">:</span><span class="token3">2</span><span class="token2">,</span> c<span class="token2">:</span><span class="token3">3</span> <span class="token2">}</span> const <span class="token2">{</span>a<span class="token2">,</span>b<span class="token2">}</span><span class="token">=</span>obj<span class="token2">;</span> 对象解析结构 let <span class="token2">[</span>a<span class="token2">,</span> b<span class="token2">,</span> c<span class="token2">]</span> <span class="token">=</span> <span class="token2">[</span><span class="token3">1</span><span class="token2">,</span> <span class="token3">2</span><span class="token2">,</span> <span class="token3">3</span><span class="token2">]</span><span class="token2">;</span>数组的解析结构 let <span class="token2">[</span>a<span class="token2">,</span> <span class="token2">.</span><span class="token2">.</span><span class="token2">.</span>b<span class="token2">]</span> <span class="token">=</span> <span class="token2">[</span><span class="token3">1</span><span class="token2">,</span> <span class="token3">2</span><span class="token2">,</span> <span class="token3">3</span><span class="token2">]</span><span class="token2">;</span> 剩余运算 a<span class="token">=</span><span class="token3">1</span> b<span class="token">=</span><span class="token2">[</span><span class="token3">2</span><span class="token2">,</span><span class="token3">3</span><span class="token2">]</span> ``` ``` 简写,当属性和值相同 ``` <pre class="calibre14">``` const person <span class="token">=</span> <span class="token2">{</span> name<span class="token2">,</span> age<span class="token2">,</span><span class="token1">fn</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">return</span> this<span class="token2">.</span>name<span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span>等价 var person <span class="token">=</span> <span class="token2">{</span> name<span class="token2">:</span> name<span class="token2">,</span> age<span class="token2">:</span> age <span class="token2">,</span> fn<span class="token2">:</span><span class="token5">function</span> <span class="token1">fn</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span> <span class="token5">return</span> this<span class="token2">.</span>name<span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span><span class="token2">;</span> ``` ``` 类 ``` <pre class="calibre14">``` class <span class="token1">Person</span> <span class="token2">{</span> <span class="token6">//构造方法</span> <span class="token1">constructor</span><span class="token2">(</span>name<span class="token2">,</span> age<span class="token2">)</span> <span class="token2">{</span> this<span class="token2">.</span>name <span class="token">=</span> name<span class="token2">;</span> this<span class="token2">.</span>age <span class="token">=</span> age<span class="token2">;</span> <span class="token2">}</span> <span class="token1">getName</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> this<span class="token2">.</span>name <span class="token2">}</span> <span class="token2">}</span> ``` ``` 模块 接口定义可以是变量,函数,类 var m=1 export {m} export default默认方法或变量 import {m} from 'm';