近期目标
2017-03-30 10:39:51 0 举报仅支持查看
AI智能生成
近期目标:在接下来的一个月内,我计划专注于提升我的专业技能和知识。我将通过参加在线课程和阅读相关书籍来实现这一目标。此外,我还计划每周至少花一个小时进行自我反思,以便更好地理解我在学习过程中的优点和需要改进的地方。我也将设定一些具体的、可衡量的目标,以便我可以跟踪我的进度。最后,我计划寻找一个导师或者加入一个专业网络,以便我可以从他人的经验中学习并获得反馈。我相信,通过这些努力,我将能够在我的专业领域中取得显著的进步。
模版推荐
作者其他创作
大纲/内容
npm
网址
docs.npmjs.com
是什么
npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准。有了npm,可以很快的找到特定服务要使用的包,进行下载、安装以及管理已经安装的包。
常用指令
清除
$ clear
查看当前目录下已安装的node包
$ npm list
查看帮助
$ npm help
访问npm的json文件
$ npm help json
自动打开一个package.json网页
查看npm使用的所有文件夹
$ npm help folders
发布一个npm包的时候,需要检验某个包名是否已存在
$ npm search packageName
会引导你创建一个package.json文件,包括名称、版本、作者这些信息等
$ npm init
查看当前包的安装路径
$ npm root
查看全局的包的安装路径
$ npm root -g
查看npm安装的版本
$npm -v
将安装包信息加入到生产阶段的依赖
npm install gulp --save-dev 或 npm install gulp -D
查看安装包的安装路径
$ npm root [-g]
npm config 管理npm的配置路径
npm config set <key> <value> [-g|--global]<br>npm config get <key><br>npm config delete <key><br>npm config list<br>npm config edit<br>npm get <key><br>npm set <key> <value> [-g|--global]
整理
安装node包<br>$ npm install<br><br>将包安装到全局环境中:<br>$ npm install -g<br><br>安装的同时,将信息写入package.json中:<br>$ npm install –save<br><br>移除全局环境中的包:<br>$ npm uninstall -g<br><br>移除指定目录的套件:<br>$ cd /path/to/the/project<br>$ npm uninstall<br><br>搜寻包:<br>$ npm search<br><br>列出全局的包:<br>$ npm ls -g<br><br>列出全局的包的详细信息:<br>$ npm ls -gl<br><br>更新node模块:<br>$ npm update -g<br><br>清理缓存:<br>$ npm cache clean<br><br>会引导你创建一个package.json文件,包括名称、版本、作者这些信息等:<br>$ npm init<br><br>查看当前目录下已安装的node包:<br>$ npm list<br><br>查看帮助命令:<br>$ npm help<br><br>查看当前包的安装路径:<br>$ npm root<br><br>查看全局的包的安装路径:<br>$ npm root -g<br><br>查看npm安装的版本:<br>$ npm -v
安装node和下载npm
在Git Bush here 输入命令行
运行node
$ node -v
运行npm
$ npm
npm安装
$ sudo npm install npm -g
有问题
修复npm权限
在Git Bush here 输入命令行
xxx
$ ls -l
xxx
$ whoami
xxx
$ sudo chown your_user file.txt
$ ls -l
XXX
$ npm config get prefix
清除
$ clear
在本地安装npm包
输入命令行
XXX
$ npm install grunt-cli -g
XXXX
$ npm install lodash
XXX
$ ls
XXX
$ ls node_modules/
子主题
$ vi app.js
子主题
$ node app.js
子主题
$ vi package.json
less
less是一门css预处理语言,它扩充了css语言,增加了诸如变量、混合、函数等功能
less网站:less.bootcss.com
JS引入less.min.js
在link标签中rel属性的值为:stylesheet/less href属性的值为:xxx.less
less语法学习
less支持所有的css语法
less支持多行和单行注释,单行注释不会编译到css文件
css只支持多行注释
less有变量概念
声明变量
@变量名:值;
声明一个变量可以在复用
使用变量
选择器{样式:@变量名;}
less支持边变量的混入
选择器1{样式;}
选择器2{选择器1;样式;}
会被译为
选择器1{样式1}
选择器1 选择器2{样式2}
less可执行变量的操作
加/减/乘/除/取余
Less为样式提供了几十个可用的函数
ceil( )<br> floor( )<br> percentage( 5/10 ) // 50%<br> round( )<br> lighten(@c, 20%) 颜色变淡指定的百分比<br> darken(@c, 30%) 颜色变暗指定的百分比<br> ....<br>
Less支持文件包含指令
Less也提供了@import指令,可用于包含其它的Less文件,推荐使用!—— Less的文件包含是在服务器端执行的文件拼合,客户端的一次请求就可以获得所有样式
通过修改Bootstrap的Less源文件实现定制
定制目标:
(1)Bootstrap瘦身,删除不必要的样式
注释掉bootstrap.less中不需要的@import即可
(2)修改Boostrap默认的样式值,实现粗粒度定制
修改variables.less中变量的值即可
(3)修改Boostrap组件的细节样式,实现细粒度定制<br>
修改特定的组件对应的.less文件,如dropdown.less
输入命令行
安装
$ npm install -g less
调用less编译器
$ lessc styles.less
输出重定向到一个文件
$ lessc styles.less > styles.css
git知识点1
学习网站:www.liaoxuefeng.com
分布式版本控制系统
git安装后名,输入命令行
$ git config --global user.name "your name"
$ git config --global user.email "xxx@xxx.com"
创建仓库(repository)
安装git
$ git
创建仓库的时候,需选择一个合适的地方,创建一个空目录
$ mkdir learngit
$ cd learngit
$pwd
会显示所在的路径
通过git init命令把这个目录变成git可以管理的仓库(初始化一个git仓库)
$ git init
瞬间将空的一个仓库建好,这是git跟踪管理仓库的,不要乱改
如果你没有看到仓库,那么仓库的隐藏的,可以用指令看见
$ ls -ah
把文件添加到仓库
在learngit目录下新建一个readme.txt文件
添加自定义内容
注意:windows自带的记事本运行不出来
添加文佳到git仓库,分两步
$ git add filel.txt
使用这个命令,告诉git,把文件添加到仓库
$ git commit -m "xxx"
-m输入的是本次提交的说明
使用这个命令告诉git,把文件提交到仓库
修改文件
随时掌握仓库的状态
$ git status
查看修改内容
$ git diff
版本回退
HEAD指向的版本就是当前版本,git 允许在版本的历史中穿梭
$ git reset --hard commit_id
穿梭前,用 $ git log 查看提交历史,以便确认回退到哪个版本
要返回未来,用git reflog查看命令历史
图片解释
<br>
工作区:learngit文件夹
版本库:Respository
工作区有一个隐藏目录.git,就是版本库
版本库最重要的就是stage暂存区
git自动创建第一个分支master
以及指向master的一个指针HEAD
git add命令实际上就是把要提交的所有修改放到暂存区(stage)
执行 git commit就可以一次性把暂存区的所有修改提交到分支master
一旦提交后,工作区就是干净的
<br>
丢掉工作区的修改
$ git checkout --files.txt
两种情况
1.修改后还没有被放入暂存区
2.已经加入到暂存区,又做了修改
用$ git reset HEAD file 也可以将暂存区的修改撤销掉,重新放回工作区
适用场景:当你不但改乱了工作区某个文件的内容,还添加了暂存区时,用这个命令
查看工作区文档内容
$ cat files.txt
删除文件
$ git rm files.txt
工作区和版本库就不一致了,再用git commit就可以了
误删时一键还原
$ git checkout -- files.txt
git知识点2
远程仓库
注册github,就可以免费获得git远程仓库
由于本地的git仓库和 github仓库之间的传输是通过SSH加密的,所以需要设置
第1步
创建SSH Key
在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步
如果没有,打开Shell(Windows下打开Git Bash),创建SSH Key:
$ ssh-keygen -t rsa -C "youremail@example.com"
如果一切顺利的话,可以在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。
第2步
登陆GitHub,打开“Account settings”,“SSH Keys”页面:
然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容:
点“Add Key”,你就应该看到已经添加的Key:
为什么GitHub需要SSH Key呢?因为GitHub需要识别出你推送的提交确实是你推送的,而不是别人冒充的,而Git支持SSH协议,所以,GitHub只要知道了你的公钥,就可以确认只有你自己才能推送。
添加远程库
现在的情景是,你已经在本地创建了一个Git仓库后,又想在GitHub创建一个Git仓库,并且让这两个仓库进行远程同步,这样,GitHub上的仓库既可以作为备份,又可以让其他人通过该仓库来协作,真是一举多得。
首先,登陆GitHub,然后,在右上角找到“Create a new repo”按钮,创建一个新的仓库:
在Repository name填入learngit,其他保持默认设置,点击“Create repository”按钮,就成功地创建了一个新的Git仓库:
目前,在GitHub上的这个learngit仓库还是空的,GitHub告诉我们,可以从这个仓库克隆出新的仓库,也可以把一个已有的本地仓库与之关联,然后,把本地仓库的内容推送到GitHub仓库。<br><br>现在,我们根据GitHub的提示,在本地的learngit仓库下运行命令:
$ git remote add origin git@github.com:michaelliao/learngit.git
请千万注意,把上面的michaelliao替换成你自己的GitHub账户名,否则,你在本地关联的就是我的远程库
把本地的所有内容推送到远程库上
$ git push -u origin master
把本地库的内容推送到远程,用git push命令,实际上是把当前分支master推送到远程。<br><br>由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。<br><br>推送成功后,可以立刻在GitHub页面中看到远程库的内容已经和本地一模一样:
以后再提交不用加-u
从远程克隆
要克隆一个仓库,首先必须知道仓库的地址,然后使用git clone命令克隆。<br><br>Git支持多种协议,包括https,但通过ssh支持的原生git协议速度最快。
git最终整理
我们先看对git操作的简单分类,如下——<br><br>远程仓库类:git clone/remote/pull/push/rebase/fetch<br>分支类(包括tag):git branch/tag/checkout/stash<br>信息查阅:git status/log/diff<br>本地常规操作:git add/commit/rm/merge/reset
按照如下四个步骤上传到名为:flexSupplement的仓库中 git init (初始化本地仓库)<br> git add . (将本地所有文件加到仓库里)<br> git commit -m "message" (设置提交信息)<br> git remote add origin git@github.com:sunningcarryhaha/flexSupplement.git(本地仓库链接远程仓库)<br> git push -u origin master (push文件到仓库中)<br>上传成功后是这个效果<br><br>作者:carryhug<br>链接:https://www.zhihu.com/question/24156818/answer/111450498<br>来源:知乎<br>著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
vueJS框架的学习
是一套构建用户界面的渐进式框架
自底向上增加开发的设计
核心库只关注视图层
安装:$ npm install vue
安装全局 vue-cli
$ npm install --global vue-cli
创建一个基于webpack模板的新项目
$ vue init webpack my-project
安装依赖
$cd my-project
$npm install
$npm run dev
语法
两种绑定方式
{{ }}
<div id="app"><br> {{ message }}<br></div>
var app = new Vue({<br> el: '#app',<br> data: {<br> message: 'Hello Vue!'<br> }<br>})
v-
<div id="app-2"><br> <span v-bind:title="message"><br> 鼠标悬停几秒钟查看此处动态绑定的提示信息!<br> </span><br></div>
var app2 = new Vue({<br> el: '#app-2',<br> data: {<br> message: '页面加载于 ' + new Date()<br> }<br>})
vuejs的核心:是一个允许采用简洁的模板语法声明式的将数据渲染进DOM
条件与循环
条件
<div id="app-3"><br> <p v-if="seen">现在你看到我了</p><br></div>
循环
var app3 = new Vue({<br> el: '#app-3',<br> data: {<br> seen: true<br> }<br>})
seen:false
效果就看不到了
常用学习网站
less中文网
http://lesscss.cn/
iView
https://www.iviewui.com/components/upload
效果源代码网站
http://www.swiper.com.cn
axios文档
https://github.com/mzabriskie/axios
ECMAScript6
http://es6.ruanyifeng.com/
github
<a href="https://github.com/waylau/books-collection">https://github.com/waylau/books-collection</a>
vue文件介绍
http://www.tuicool.com/articles/mYVvmyA
ECMAScript6语法
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">const</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">的作用域与</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">let</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">命令相同:只在声明所在的块级作用域内有效。</span>
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">const</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">声明的常量,也与</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">let</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">一样不可重复声明。</span>
声明变量
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">ES5 只有两种声明变量的方法:</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">var</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">命令和</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">function</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">命令</span>
ES6除了添加let和const命令,后面章节还会提到,另外两种声明变量的方法:import命令和class命令。
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">var</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">命令和</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">function</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">命令声明的全局变量,依旧是顶层对象的属性;</span>
var a=1;<br>window.a //1
注意:如果在Node的REPL环境,可以写成global.a<br> 或者采用通用方法,写成this.a<br>
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">let</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">命令、</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">const</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">命令、</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">class</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">命令声明的全局变量,不属于顶层对象的属性</span>
let b=1;<br>window.b //underfined<br>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">在所有环境下,</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">global</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">都是存在的,都可以从它拿到顶层对象的方法。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">// CommonJS的写法
</font></span><span class="token function"><font size="2">require</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'system.global/shim'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">
// ES6模块的写法
</font></span><font size="2">import shim from </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'system.global/shim'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><font size="2"> </font><span class="token function"><font size="2">shim</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span></code></pre>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">// CommonJS的写法
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> global </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token function"><font size="2">require</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'system.global'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">
// ES6模块的写法
</font></span><font size="2">import getGlobal from </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'system.global'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">const global </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token function"><font size="2">getGlobal</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span></code></pre>
解构(为变量赋值)
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><font size="2">x</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> y</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> z</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">new</font></span><font size="2"> </font><span class="token class-name"><font size="2">Set</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'b'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'c'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">x</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "a"</font></span></code></pre>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2"> foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> bar </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2"> foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">"aaa"</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> bar</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">"bbb"</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">foo</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "aaa"
</font></span><font size="2">bar</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "bbb"</font></span></code></pre>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2"> foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> baz </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2"> foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'aaa'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> bar</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'bbb'</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">baz</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "aaa"
</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> obj </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2"> first</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'hello'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> last</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'world'</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2"> first</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> f</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> last</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> l </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> obj</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">f</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 'hello'
</font></span><font size="2">l</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 'world'</font></span></code></pre>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2"> foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> baz </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2"> foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">"aaa"</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> bar</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">"bbb"</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">baz</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "aaa"
</font></span><font size="2">foo</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // error: foo is not defined</font></span></code></pre>
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">foo</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">是匹配的模式,</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">baz</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">才是变量。真正被赋值的是变量</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">baz</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">,而不是模式</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">foo</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2">foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2">foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 成功
</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> baz</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2">bar</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> baz</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2">bar</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 成功</font></span></code></pre>
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">let</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">命令下面一行的圆括号是必须的,否则会报错。因为解析器会将起首的大括号,理解成一个代码块,而不是赋值语句。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> obj </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> arr </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">
</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2"> foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> obj</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">prop</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> bar</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> arr</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2"> foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">123</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> bar</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token boolean" style="color: rgb(174, 129, 255);"><font size="2">true</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">
obj</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // {prop:123}
</font></span><font size="2">arr</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // [true]</font></span></code></pre>
解构(为变量赋值)
1.数组的解构赋值
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><font size="2">foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><font size="2">bar</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> baz</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">foo</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 1
</font></span><font size="2">bar</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 2
</font></span><font size="2">baz</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 3
</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> third</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">"foo"</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">"bar"</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">"baz"</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">third</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "baz"
</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><font size="2">x</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> y</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">x</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 1
</font></span><font size="2">y</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 3
</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><font size="2">head</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">tail</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">4</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">head</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 1
</font></span><font size="2">tail</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // [2, 3, 4]
</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><font size="2">x</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> y</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">z</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">x</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "a"
</font></span><font size="2">y</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // undefined
</font></span><font size="2">z</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // []</font></span></code></pre>
2.对象的解构赋值
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> obj </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> arr </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">
</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2"> foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> obj</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">prop</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> bar</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> arr</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2"> foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">123</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> bar</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token boolean" style="color: rgb(174, 129, 255);"><font size="2">true</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">
obj</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // {prop:123}
</font></span><font size="2">arr</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // [true]</font></span></code></pre>
3.字符串的解构赋值
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">const </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><font size="2">a</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> b</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> c</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> d</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> e</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'hello'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">a</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "h"
</font></span><font size="2">b</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "e"
</font></span><font size="2">c</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "l"
</font></span><font size="2">d</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "l"
</font></span><font size="2">e</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "o"</font></span></code></pre>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2">length </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> len</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'hello'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">len</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 5</font></span></code></pre>
4.数值和布尔值的解构赋值
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2">toString</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> s</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">123</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">s </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">===</font></span><font size="2"> Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">prototype</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">toString</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // true
</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2">toString</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> s</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token boolean" style="color: rgb(174, 129, 255);"><font size="2">true</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">s </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">===</font></span><font size="2"> Boolean</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">prototype</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">toString</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // true</font></span></code></pre>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">解构赋值时,如果等号右边是数值和布尔值,则会先转为对象</span>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">解构赋值的规则是,只要等号右边的值不是对象或数组,就先将其转为对象</span>
5.函数参数的解构赋值
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><font size="2"> </font><span class="token function"><font size="2">add</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><font size="2">x</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> y</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">return</font></span><font size="2"> x </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">+</font></span><font size="2"> y</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span>
<font size="2">
</font><span class="token function"><font size="2">add</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 3</font></span></code></pre>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">4</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">map</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><font size="2">a</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> b</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">></font></span><font size="2"> a </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">+</font></span><font size="2"> b</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// [ 3, 7 ]</font></span></code></pre>
注意:可以使用圆括号的情况
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><font size="2">b</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 正确
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2"> p</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><font size="2">d</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 正确
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><font size="2">parseInt</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">prop</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 正确</font></span></code></pre>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">上面三行语句都可以正确执行,因为首先它们都是赋值语句,而不是声明语句;其次它们的圆括号都不属于模式的一部分。第一行语句中,模式是取数组的第一个成员,跟圆括号无关;第二行语句中,模式是p,而不是d;第三行语句与第一行语句的性质一致。</span>
对unicode码的处理
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">"\u{20BB7}"</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// "𠮷"
</font></span>
<span class="token string" style="color: rgb(166, 226, 46);"><font size="2">"\u{41}\u{42}\u{43}"</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// "ABC"
</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> hello </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">123</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">hell\u</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2">6F</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 123
</font></span>
<span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'\u{1F680}'</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">===</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'\uD83D\uDE80'</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// true</font></span></code></pre>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">只要将码点放入大括号,就能正确解读该字符。</span>
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">codePointAt</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法返回的是码点的十进制值,如果想要十六进制的值,可以使用</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">toString</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法转换一下</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> s </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'𠮷a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">
s</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">codePointAt</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">toString</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">16</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "20bb7"
</font></span><font size="2">s</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">codePointAt</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">toString</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">16</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "61"</font></span></code></pre>
对正则表达式的处理
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'宋体';font-size:12.0pt;"><span style="color:#cc7832;font-weight:bold;">const </span>reg = <span style="color:#6a8759;">/^[0-9A-Za-z]{6,20}$/</span><span style="color:#cc7832;">;</span></pre>
数值的扩展
1.二进制和八进制的表示法
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">ES6 提供了二进制和八进制数值的新的写法,分别用前缀</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">0b</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">(或</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">0B</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">)和</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">0o</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">(或</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">0O</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">)表示。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">0b111110111 </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">===</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">503</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // true
</font></span><font size="2">0o767 </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">===</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">503</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // true</font></span></code></pre>
2.Number.isFinite(),Number.isNaN()
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">Number.isFinite()</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">用来检查一个数值是否为有限的(finite)。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isFinite</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">15</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // true
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isFinite</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0.8</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // true
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isFinite</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">NaN</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // false
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isFinite</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">Infinity</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // false
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isFinite</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">-</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">Infinity</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // false
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isFinite</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'foo'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // false
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isFinite</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'15'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // false
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isFinite</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token boolean" style="color: rgb(174, 129, 255);"><font size="2">true</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // false</font></span></code></pre>
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">Number.isNaN()</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">用来检查一个值是否为</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">NaN</font></code>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isNaN</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">NaN</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // true
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isNaN</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">15</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // false
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isNaN</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'15'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // false
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isNaN</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token boolean" style="color: rgb(174, 129, 255);"><font size="2">true</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // false
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isNaN</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">9</font></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">/</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">NaN</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // true
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isNaN</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'true'</font></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">/</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // true
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isNaN</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'true'</font></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">/</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'true'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // true</font></span></code></pre>
3.Number.parseInt(),Number.parseFloat()
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">ES6将全局方法</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">parseInt()</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">和</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">parseFloat()</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">,移植到Number对象上面,行为完全保持不变。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">parseInt</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'12.34'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 12
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">parseFloat</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'123.45#'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 123.45</font></span></code></pre>
4.Number.isInteger()
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">Number.isInteger()</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">用来判断一个值是否为整数。需要注意的是,在JavaScript内部,整数和浮点数是同样的储存方法,所以3和3.0被视为同一个值。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isInteger</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">25</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // true
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isInteger</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">25.0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // true
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isInteger</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">25.1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // false
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isInteger</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">"15"</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // false
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">isInteger</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token boolean" style="color: rgb(174, 129, 255);"><font size="2">true</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // false</font></span></code></pre>
5.Number.EPSILON
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">引入一个这么小的量的目的,在于为浮点数计算,设置一个误差范围。我们知道浮点数计算是不精确的。</span>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">但是如果这个误差能够小于</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">Number.EPSILON</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">,我们就可以认为得到了正确结果。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">EPSILON</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// 2.220446049250313e-16
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">EPSILON</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">toFixed</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">20</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// '0.00000000000000022204'</font></span></code></pre>
6.安全整数和Number.isSafeInteger()
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">ES6引入了</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">Number.MAX_SAFE_INTEGER</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">和</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">Number.MIN_SAFE_INTEGER</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">这两个常量,用来表示这个范围的上下限</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">MAX_SAFE_INTEGER </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">===</font></span><font size="2"> Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">pow</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">53</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">-</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// true
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">MAX_SAFE_INTEGER </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">===</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">9007199254740991</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// true
</font></span>
<font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">MIN_SAFE_INTEGER </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">===</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">-</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">MAX_SAFE_INTEGER</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// true
</font></span><font size="2">Number</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">MIN_SAFE_INTEGER </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">===</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">-</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">9007199254740991</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// true</font></span></code></pre>
7.Math对象的扩展
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">ES6在Math对象上新增了17个与数学相关的方法。所有这些方法都是静态方法,只能在Math对象上调用。</span>
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">Math.trunc</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法用于去除一个数的小数部分,返回整数部分。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">trunc</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">4.1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 4
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">trunc</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">4.9</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 4
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">trunc</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">-</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">4.1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // -4
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">trunc</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">-</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">4.9</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // -4
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">trunc</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">-</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0.1234</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // -0</font></span></code></pre>
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">Math.sign</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法用来判断一个数到底是正数、负数、还是零。</span>
<p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">它会返回五种值。</p><ul style="display: block; list-style-type: none; padding-top: 0.5rem; padding-bottom: 0.5rem; color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><li style="text-indent: -5px; font-size: 0.8rem;">参数为正数,返回+1;</li><li style="text-indent: -5px; font-size: 0.8rem;">参数为负数,返回-1;</li><li style="text-indent: -5px; font-size: 0.8rem;">参数为0,返回0;</li><li style="text-indent: -5px; font-size: 0.8rem;">参数为-0,返回-0;</li><li style="text-indent: -5px; font-size: 0.8rem;">其他值,返回NaN。</li></ul>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">sign</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">-</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">5</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // -1
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">sign</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">5</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // +1
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">sign</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // +0
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">sign</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">-</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // -0
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">sign</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">NaN</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // NaN
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">sign</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'foo'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // NaN
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">sign</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><font size="2"> </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // NaN</font></span></code></pre>
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">Math.cbrt</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法用于计算一个数的立方根。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">cbrt</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">-</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // -1
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">cbrt</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 0
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">cbrt</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 1
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">cbrt</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 1.2599210498948734</font></span></code></pre>
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">Math.clz32</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法返回一个数的32位无符号整数形式有多少个前导0。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">clz32</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 32
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">clz32</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 31
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">clz32</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1000</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 22
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">clz32</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">0b01000000000000000000000000000000</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 1
</font></span><font size="2">Math</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">clz32</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">0b00100000000000000000000000000000</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 2</font></span></code></pre>
数组的扩展
1.Array.from()
将类数组对象和可遍历的对象转为真正的数组
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> arrayLike </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'0'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span>
<font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'1'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'b'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span>
<font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'2'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'c'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span>
<font size="2"> length</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">
// ES5的写法
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> arr1 </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">slice</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">call</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">arrayLike</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // ['a', 'b', 'c']
</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// ES6的写法
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> arr2 </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> Array</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">from</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">arrayLike</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // ['a', 'b', 'c']</font></span></code></pre>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">// NodeList对象
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> ps </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> document</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">querySelectorAll</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'p'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">Array</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">from</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">ps</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">forEach</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><font size="2">p</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> console</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">p</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">
// arguments对象
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><font size="2"> </font><span class="token function"><font size="2">foo</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> args </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> Array</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">from</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">arguments</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2"> </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // ...
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span></code></pre>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">对于还没有部署该方法的浏览器,可以用</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">Array.prototype.slice</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法替代。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">const toArray </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">></font></span>
<font size="2"> Array</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">from </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">?</font></span><font size="2"> Array</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">from </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> obj </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">></font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">slice</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">call</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">obj</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span></code></pre>
2.Array.of()
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">Array.of</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法用于将一组值,转换为数组</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">Array</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">of</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">11</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">8</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // [3,11,8]
</font></span><font size="2">Array</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">of</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // [3]
</font></span><font size="2">Array</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">of</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">length</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 1</font></span></code></pre>
3.数组实例copyWithin()
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">数组实例的</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">copyWithin</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法,在当前数组内部,将指定位置的成员复制到其他位置(会覆盖原有成员),然后返回当前数组。也就是说,使用这个方法,会修改当前数组。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">Array</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">prototype</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">copyWithin</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">target</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> start </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> end </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">this</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">length</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span></code></pre>
<p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">它接受三个参数。</p><ul style="display: block; list-style-type: none; padding-top: 0.5rem; padding-bottom: 0.5rem; color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><li style="text-indent: -5px; font-size: 0.8rem;">target(必需):从该位置开始替换数据。</li><li style="text-indent: -5px; font-size: 0.8rem;">start(可选):从该位置开始读取数据,默认为0。如果为负值,表示倒数。</li><li style="text-indent: -5px; font-size: 0.8rem;">end(可选):到该位置前停止读取数据,默认等于数组长度。如果为负值,表示倒数。</li></ul><p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">这三个参数都应该是数值,如果不是,会自动转为数值。</p>
4.数组实例的find()和findindex()
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">数组实例的</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">find</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法,用于找出第一个符合条件的数组成员。它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">true</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">的成员,然后返回该成员。如果没有符合条件的成员,则返回</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">undefined</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">4</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">-</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">5</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">10</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">find</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><font size="2">n</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">></font></span><font size="2"> n </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2"><</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// -5</font></span></code></pre>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">数组实例的</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">findIndex</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法的用法与</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">find</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法非常类似,返回第一个符合条件的数组成员的位置,如果所有成员都不符合条件,则返回</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">-1</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">5</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">10</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">15</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">findIndex</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><font size="2">value</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> index</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> arr</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">return</font></span><font size="2"> value </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">></font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">9</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 2</font></span></code></pre>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">这两个方法都可以接受第二个参数,用来绑定回调函数的</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">this</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">对象。</span>
5.数组实例的fill()
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">fill</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法使用给定值,填充一个数组。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'b'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'c'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">fill</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">7</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// [7, 7, 7]
</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">new</font></span><font size="2"> </font><span class="token class-name"><font size="2">Array</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">fill</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">7</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// [7, 7, 7]</font></span></code></pre>
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">fill</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法还可以接受第二个和第三个参数,用于指定填充的起始位置和结束位置。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'b'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'c'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">fill</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">7</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// ['a', 7, 'c']</font></span></code></pre>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">上面代码表示,</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">fill</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法从1号位开始,向原数组填充7,到2号位之前结束。</span>
6.数组实例的entries(),keys()和values()
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">ES6提供三个新的方法——</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">entries()</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">,</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">keys()</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">和</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">values()</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">——用于遍历数组.</span><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">唯一的区别是</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">keys()</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">是对键名的遍历、</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">values()</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">是对键值的遍历,</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">entries()</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">是对键值对的遍历。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">for</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> index of </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'b'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">keys</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> console</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">index</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// 0
</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">// 1
</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">for</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> elem of </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'b'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">values</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> console</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">elem</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// 'a'
</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">// 'b'
</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">for</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><font size="2">index</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> elem</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> of </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'b'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">entries</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> console</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">index</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> elem</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// 0 "a"
</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">// 1 "b"</font></span></code></pre>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">如果不使用</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">for...of</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">循环,可以手动调用遍历器对象的</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">next</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法,进行遍历。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> letter </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'b'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'c'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> entries </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> letter</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">entries</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">console</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">entries</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">next</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">value</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // [0, 'a']
</font></span><font size="2">console</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">entries</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">next</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">value</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // [1, 'b']
</font></span><font size="2">console</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">entries</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">next</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">value</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // [2, 'c']</font></span></code></pre>
7.数组实例的includes()
<font color="#c7254e" face="Consolas" size="2" style="background-color: rgb(244, 242, 249);">Array.prototype.includes</font><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法返回一个布尔值,表示某个数组是否包含给定的值,与字符串的</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">includes</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法类似</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">includes</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><font size="2"> </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // true
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">includes</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">4</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><font size="2"> </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // false
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">NaN</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">includes</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">NaN</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // true</font></span></code></pre>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">没有该方法之前,我们通常使用数组的</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">indexOf</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">方法,检查是否包含某个值。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">if</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><font size="2">arr</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">indexOf</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">el</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">!</font></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">==</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">-</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // ...
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span></code></pre>
8.数组的空位
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">数组的空位指,数组的某一个位置没有任何值。比如,</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">Array</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">构造函数返回的数组都是空位。</span>
<p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">Array.from</font></code>方法会将数组的空位,转为<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">undefined</font></code>,也就是说,这个方法不会忽略空位。</p><pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">Array</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">from</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'b'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// [ "a", undefined, "b" ]
</font></span></code></pre><p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">扩展运算符(<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">...</font></code>)也会将空位转为<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">undefined</font></code>。</p><pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'b'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// [ "a", undefined, "b" ]
</font></span></code></pre><p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><code style='background: rgb(249, 242, 244); border-radius: 2px; color: rgb(199, 37, 78); padding-right: 3px; padding-left: 3px; font-family: Consolas,"Courier New",Courier,FreeMono,monospace; font-weight: normal;'><font size="2">copyWithin()</font></code>会连空位一起拷贝。</p><pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'b'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">copyWithin</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // [,"a",,"a"]
</font></span></code></pre><p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><code style='background: rgb(249, 242, 244); border-radius: 2px; color: rgb(199, 37, 78); padding-right: 3px; padding-left: 3px; font-family: Consolas,"Courier New",Courier,FreeMono,monospace; font-weight: normal;'><font size="2">fill()</font></code>会将空位视为正常的数组位置。</p><pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">new</font></span><font size="2"> </font><span class="token class-name"><font size="2">Array</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">fill</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // ["a","a","a"]
</font></span></code></pre><p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><code style='background: rgb(249, 242, 244); border-radius: 2px; color: rgb(199, 37, 78); padding-right: 3px; padding-left: 3px; font-family: Consolas,"Courier New",Courier,FreeMono,monospace; font-weight: normal;'><font size="2">for...of</font></code>循环也会遍历空位。</p><pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> arr </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">for</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> i of arr</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> console</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// 1
</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">// 1
</font></span></code></pre><p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">上面代码中,数组<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">arr</font></code>有两个空位,<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">for...of</font></code>并没有忽略它们。如果改成<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">map</font></code>方法遍历,空位是会跳过的。</p>
<p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">entries()</font></code>、<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">keys()</font></code>、<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">values()</font></code>、<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">find()</font></code>和<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">findIndex()</font></code>会将空位处理成<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">undefined</font></code>。</p><pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">// entries()
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">entries</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // [[0,undefined], [1,"a"]]
</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// keys()
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">keys</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // [0,1]
</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// values()
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">values</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // [undefined,"a"]
</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// find()
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">find</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">x </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">></font></span><font size="2"> </font><span class="token boolean" style="color: rgb(174, 129, 255);"><font size="2">true</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // undefined
</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// findIndex()
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">findIndex</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">x </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">></font></span><font size="2"> </font><span class="token boolean" style="color: rgb(174, 129, 255);"><font size="2">true</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 0</font></span></code></pre>
函数的扩展
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">1.ES6 允许为函数的参数设置默认值,即直接写在参数定义的后面</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><font size="2"> </font><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">x</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> y </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'World'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> console</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">x</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> y</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span>
<font size="2">
</font><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'Hello'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // Hello World
</font></span><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'Hello'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'China'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // Hello China
</font></span><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'Hello'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">''</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // Hello</font></span></code></pre>
2.rest参数
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">ES6 引入 rest 参数(形式为“...变量名”),用于获取函数的多余参数,这样就不需要使用</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">arguments</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">对象了。rest 参数搭配的变量是一个数组,该变量将多余的参数放入数组中。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">// arguments变量的写法
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><font size="2"> </font><span class="token function"><font size="2">sortNumbers</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">return</font></span><font size="2"> Array</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">prototype</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">slice</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">call</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">arguments</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">sort</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span>
<span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">
// rest参数的写法
</font></span><font size="2">const sortNumbers </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">numbers</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">></font></span><font size="2"> numbers</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">sort</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span></code></pre>
3.扩展运算符
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">扩展运算符(spread)是三个点(</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">...</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">)。它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">console</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// 1 2 3
</font></span>
<font size="2">console</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">log</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">4</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">5</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// 1 2 3 4 5
</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">document</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">querySelectorAll</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'div'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// [<div>, <div>, <div>]</font></span></code></pre>
替代数组的apply方法
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">// ES5的写法
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><font size="2"> </font><span class="token function"><font size="2">f</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">x</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> y</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> z</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // ...
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> args </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">f</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">apply</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">null</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> args</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">
// ES6的写法
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><font size="2"> </font><span class="token function"><font size="2">f</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">x</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> y</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> z</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // ...
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> args </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token function"><font size="2">f</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">args</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span></code></pre>
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">另一个例子是通过</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">push</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">函数,将一个数组添加到另一个数组的尾部。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">// ES5的写法
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> arr1 </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> arr2 </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">4</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">5</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">Array</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">prototype</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">push</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">apply</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">arr1</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> arr2</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">
// ES6的写法
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> arr1 </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">0</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> arr2 </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">4</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">5</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">arr1</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">push</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">arr2</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span></code></pre>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">// ES5
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">new</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><font size="2">Date</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">bind</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">apply</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">Date</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">null</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2015</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// ES6
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">new</font></span><font size="2"> </font><span class="token class-name"><font size="2">Date</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2015</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span></code></pre>
扩展字符串的应用
1.合并数组
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">// ES5
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">concat</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">more</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// ES6
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">more</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span>
<font size="2">
</font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> arr1 </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'b'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> arr2 </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'c'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> arr3 </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'d'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'e'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">
// ES5的合并数组
</font></span><font size="2">arr1</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">concat</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">arr2</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> arr3</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// [ 'a', 'b', 'c', 'd', 'e' ]
</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// ES6的合并数组
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">arr1</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">arr2</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">arr3</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// [ 'a', 'b', 'c', 'd', 'e' ]、</font></span></code></pre>
2.与解构赋值结合
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">const </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><font size="2">first</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">rest</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">4</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">5</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">first</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // 1
</font></span><font size="2">rest </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // [2, 3, 4, 5]
</font></span>
<font size="2">const </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><font size="2">first</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">rest</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">first</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // undefined
</font></span><font size="2">rest </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // []:
</font></span>
<font size="2">const </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><font size="2">first</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">rest</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">"foo"</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">first </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "foo"
</font></span><font size="2">rest </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // []</font></span></code></pre>
3.函数的返回值
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> dateFields </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token function"><font size="2">readDateFields</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">database</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> d </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">new</font></span><font size="2"> </font><span class="token class-name"><font size="2">Date</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">dateFields</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span></code></pre>
4.字符串
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> str </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'x\uD83D\uDE80y'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">
str</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">split</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">''</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">reverse</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">join</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">''</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// 'y\uDE80\uD83Dx'
</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">str</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">reverse</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">join</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">''</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// 'y\uD83D\uDE80x'</font></span></code></pre>
5.实现了Iterator接口的对象
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> arrayLike </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'0'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'a'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span>
<font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'1'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'b'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span>
<font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'2'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'c'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span>
<font size="2"> length</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">:</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">
// TypeError: Cannot spread non-iterable object.
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">let</font></span><font size="2"> arr </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">arrayLike</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span></code></pre>
6.Map和Set结构,Generator函数
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> go </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">*</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> yield </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">1</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2"> yield </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">2</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2"> yield </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">3</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">
</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">[</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><span class="token function"><font size="2">go</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">]</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // [1, 2, 3]</font></span></code></pre>
4.严格模式
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">两种方法可以规避这种限制。第一种是设定全局性的严格模式,这是合法的。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'use strict'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">
</font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><font size="2"> </font><span class="token function"><font size="2">doSomething</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><font size="2">a</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> b </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> a</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> </font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // code
</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span></code></pre>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">const doSomething </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> </font><span class="token string" style="color: rgb(166, 226, 46);"><font size="2">'use strict'</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">return</font></span><font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><font size="2">value </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">42</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">return</font></span><font size="2"> value</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span></code></pre>
5.name属性
<span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">函数的</span><code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><font size="2">name</font></code><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">属性,返回该函数的函数名。</span>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> f </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">
// ES5
</font></span><font size="2">f</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">name</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // ""
</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// ES6
</font></span><font size="2">f</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">name</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "f"</font></span></code></pre>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> f </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">
// ES5
</font></span><font size="2">f</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">name</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // ""
</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// ES6
</font></span><font size="2">f</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">name</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "f"</font></span></code></pre>
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><font size="2">const bar </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><font size="2"> </font><span class="token function"><font size="2">baz</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2">
// ES5
</font></span><font size="2">bar</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">name</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "baz"
</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// ES6
</font></span><font size="2">bar</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">.</font></span><font size="2">name</font><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true"><font size="2"> // "baz"</font></span></code></pre>
6.箭头函数
<pre class=" language-javascript" style='overflow: auto; font-family: Consolas, Monaco, "Andale Mono", monospace; font-size: 15.36px; color: rgb(255, 255, 255); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: 0px; word-break: normal; tab-size: 4; hyphens: none; padding: 10px 1em 10px 0.7rem; margin: 0.5em auto; border-radius: 5px; background: rgb(17, 17, 17); line-height: 1.2; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;'><code class=" language-javascript" style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(166, 226, 46); text-shadow: rgba(0, 0, 0, 0.3) 0px 1px; direction: ltr; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; tab-size: 4; hyphens: none; font-weight: normal; background: rgb(17, 17, 17); border-radius: 2px;'><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> f </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">></font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">5</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// 等同于
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> f </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span><font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">return</font></span><font size="2"> </font><span class="token number" style="color: rgb(174, 129, 255);"><font size="2">5</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<font size="2">
</font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> sum </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><font size="2">num1</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> num2</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">></font></span><font size="2"> num1 </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">+</font></span><font size="2"> num2</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span><span class="token comment" style="color: rgb(117, 113, 94);" spellcheck="true">
<font size="2">// 等同于
</font></span><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">var</font></span><font size="2"> sum </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">=</font></span><font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">function</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">(</font></span><font size="2">num1</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">,</font></span><font size="2"> num2</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">)</font></span><font size="2"> </font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">{</font></span>
<font size="2"> </font><span class="token keyword" style="color: rgb(102, 217, 239);"><font size="2">return</font></span><font size="2"> num1 </font><span class="token operator" style="color: rgb(249, 38, 114);"><font size="2">+</font></span><font size="2"> num2</font><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span>
<span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">}</font></span><span class="token punctuation" style="color: rgb(248, 248, 242);"><font size="2">;</font></span></code></pre>
注意
<p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">箭头函数有几个使用注意点。</p><p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">(1)函数体内的<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">this</font></code>对象,就是定义时所在的对象,而不是使用时所在的对象。</p><p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">(2)不可以当作构造函数,也就是说,不可以使用<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">new</font></code>命令,否则会抛出一个错误。</p><p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">(3)不可以使用<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">arguments</font></code>对象,该对象在函数体内不存在。如果要用,可以用Rest参数代替。</p><p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">(4)不可以使用<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">yield</font></code>命令,因此箭头函数不能用作Generator函数。</p><p style="color: rgb(51, 51, 51); font-family: Verdana, Arial; font-size: 15.36px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">上面四点中,第一点尤其值得注意。<code style='font-family: Consolas, "Courier New", Courier, FreeMono, monospace; font-size: 0.7rem; color: rgb(199, 37, 78); font-weight: normal; background: rgb(249, 242, 244); border-radius: 2px; padding-left: 3px; padding-right: 3px;'><font size="2">this</font></code>对象的指向是可变的,但是在箭头函数中,它是固定的。</p>
Collect
Get Started
Collect
Get Started
Collect
Get Started
Collect
Get Started
评论
0 条评论
下一页