<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,常用于前后端之间的数据传输。JSON 的书写格式主要有以下几种:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">对象格式(Object)</p><pre class="brush:as3;toolbar:false">{ "name": "John", "age": 30, "isStudent": true, "hobbies": ["reading", "running", "swimming"], "address": { "street": "123 Main St", "city": "New York", "country": "USA" } }</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">数组格式(Array)</p><pre class="brush:as3;toolbar:false">[ "apple", "banana", "orange", "grape"]</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">值格式(Value)</p><pre class="brush:as3;toolbar:false">"Hello, World!"</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">空值格式(Null)</p><pre class="brush:as3;toolbar:false">null</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">JSON <span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: "PingFang SC";">数据格式的优点包括:易于阅读和编写,易于解析和生成,支持多种编程语言。在</span> Web <span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: "PingFang SC";">开发中,我们通常会使用</span> JavaScript <span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: "PingFang SC";">的</span> JSON.parse() <span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: "PingFang SC";">方法将</span> JSON <span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: "PingFang SC";">数据转换为对象格式,使用</span> JSON.stringify() <span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: "PingFang SC";">方法将</span> JavaScript <span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: "PingFang SC";">对象转换为</span> JSON <span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: "PingFang SC";">数据格式。</span></p><p><br/></p>
文章列表
<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Webpack 是一个强大的打包工具,可以帮助我们将多个模块打包成一个或多个 JavaScript 文件。下面是一个基本的 Webpack 配置示例,可以用于构建一个简单的 JavaScript 项目:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; min-height: 16px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">安装 Webpack 和相关依赖</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在项目的根目录下打开命令行窗口,执行以下命令来安装 Webpack 和相关依赖:</p><pre class="brush:as3;toolbar:false">npm install webpack webpack-cli webpack-dev-server html-webpack-plugin --save-dev</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">其中,webpack 是 Webpack 的核心依赖,webpack-cli 是 Webpack 的命令行工具,webpack-dev-server 是 Webpack 的开发服务器,html-webpack-plugin 是 Webpack 的 HTML 模板插件。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; min-height: 16px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">创建 Webpack 配置文件</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在项目的根目录下创建一个名为 webpack.config.js 的文件,然后添加以下代码:</p><pre class="brush:as3;toolbar:false">const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './src/index.js', // 入口文件 output: { // 输出文件 path: path.resolve(__dirname, 'dist'), filename: 'bundle.js' }, module: { // 模块配置 rules: [ { test: /\.js$/, // 匹配 JavaScript 文件 exclude: /node_modules/, // 排除 node_modules 目录 use: { loader: 'babel-loader' // 使用 babel-loader 处理 JavaScript 文件 } } ] }, plugins: [ // 插件配置 new HtmlWebpackPlugin({ template: './src/index.html' // 使用 index.html 作为 HTML 模板 }) ], devServer: { // 开发服务器配置 contentBase: './dist' // 设置服务器的根目录为 dist 目录 } };</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">上面的代码中,entry 指定了入口文件,output 指定了输出文件,module.rules 定义了如何处理 JavaScript 文件,plugins 配置了 Webpack 的插件,devServer 配置了 Webpack 的开发服务器。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; min-height: 16px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">创建 Babel 配置文件</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在项目的根目录下创建一个名为 .babelrc 的文件,然后添加以下代码:</p><pre class="brush:as3;toolbar:false">{ "presets": ["@babel/preset-env"] }</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">上面的代码指定了使用 @babel/preset-env 来处理 JavaScript 文件。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; min-height: 16px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">创建 JavaScript 文件和 HTML 文件</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在 src 目录下创建一个名为 index.js 的文件,然后添加以下代码:</p><pre class="brush:as3;toolbar:false">const sum = (a, b) => { return a + b; }; console.log(sum(1, 2));</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在 src 目录下创建一个名为 index.html 的文件,然后添加以下代码:</p><pre class="brush:as3;toolbar:false"><!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Webpack Demo</title> </head> <body> <script src="bundle.js"></script> </body> </html></pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">执行打包命令</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在命令行中执行以下命令来启动 Webpack 开发服务器:</p><pre class="brush:as3;toolbar:false">npx webpack serve --mode development</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">执行完成后,可以在浏览器中打开 http://localhost:8080 来查看项目效果。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "Helvetica Neue"; min-height: 16px; white-space: normal;"><br/></p><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 14px; line-height: normal; font-family: "PingFang SC"; white-space: normal;">以上是一个基本的<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue";"> Webpack </span>配置示例,你可以根据项目需要进行相应的修改和扩展。</p><p><br/></p>
<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Webpack 是一种模块化打包工具,可以将多个 JavaScript 文件打包成一个或多个文件,以便在浏览器中加载。Babel 是一种 JavaScript 编译器,可以将 ES6 语法转换成浏览器可以支持的 ES5 语法。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Webpack 和 Babel 的结合使用,可以实现将 ES6 语法转换成浏览器可以支持的 ES5 语法,并将多个 JavaScript 文件打包成一个或多个文件的功能。下面是使用 Webpack 和 Babel 的一些常见步骤:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">安装 Webpack 和 Babel</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在命令行中使用以下命令安装 Webpack 和 Babel:</p><pre class="brush:as3;toolbar:false">npm install webpack webpack-cli babel-loader @babel/core @babel/preset-env --save-dev</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">创建 Webpack 配置文件</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在项目根目录下创建一个名为 webpack.config.js 的文件,然后添加以下代码:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><pre class="brush:as3;toolbar:false">const path = require('path'); module.exports = { entry: './src/index.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js' }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env'] } } } ] } };</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">上面的代码指定了入口文件(entry)、输出文件(output)以及如何处理 JavaScript 文件(module.rules)。其中使用 babel-loader 和 @babel/preset-env 来处理 JavaScript 文件。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">创建 Babel 配置文件</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在项目根目录下创建一个名为 .babelrc 的文件,然后添加以下代码:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><pre class="brush:as3;toolbar:false">{ "presets": ["@babel/preset-env"] }</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">上面的代码指定了使用 @babel/preset-env 来处理 JavaScript 文件。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">创建 JavaScript 文件</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在 src 目录下创建一个名为 index.js 的文件,然后添加以下代码:</p><pre class="brush:as3;toolbar:false">const sum = (a, b) => { return a + b; }; console.log(sum(1, 2));</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">上面的代码是一个使用 ES6 语法定义的函数。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">执行打包命令</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在命令行中使用以下命令来执行打包:</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">npx webpack --mode development</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">上面的命令会执行 Webpack 的打包操作,并将打包结果输出到 dist 目录下的 bundle.js 文件中。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">执行完成后,可以在浏览器中打开 index.html 文件,并查看浏览器控制台的输出结果,应该可以看到 sum 函数的结果为 3。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "PingFang SC"; white-space: normal;"><span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue";">Webpack </span>和<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue";"> Babel </span>的结合使用可以帮助开发者在浏览器中使用<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue";"> ES6 </span>语法,提高开发效率和代码质量。</p><p><br/></p>
<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">ES6 中的 Promise 是一种处理异步操作的机制,它可以避免回调地狱,使得异步代码更加易读和可维护。下面是如何运用 ES6 Promise 进行异步编程的一些常见方式:</p><pre class="brush:as3;toolbar:false">Promise.resolve() 和 Promise.reject() Promise.resolve() 和 Promise.reject() 是两个常用的静态方法,它们可以用来快速地创建一个已经成功或已经失败的 Promise 对象。比如: const p1 = Promise.resolve('成功'); const p2 = Promise.reject('失败');</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Promise.then() 和 Promise.catch()</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Promise.then() 方法用来处理 Promise 对象的成功情况,Promise.catch() 方法用来处理 Promise 对象的失败情况。可以通过链式调用的方式来处理 Promise 对象的结果,比如:</p><pre class="brush:as3;toolbar:false">const p = new Promise((resolve, reject) => { // 异步操作 setTimeout(() => { const result = Math.random() < 0.5 ? '成功' : '失败'; if (result === '成功') { resolve(result); } else { reject(result); } }, 1000); }); p.then((result) => { console.log(result); // 处理成功的情况 }).catch((result) => { console.log(result); // 处理失败的情况 });</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Promise.all()</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Promise.all() 方法可以同时处理多个 Promise 对象,等待所有 Promise 对象都完成后,再将它们的结果按照顺序依次返回。如果其中任何一个 Promise 对象失败了,则整个 Promise.all() 也会失败。比如:</p><pre class="brush:as3;toolbar:false">const p1 = Promise.resolve('第一个 Promise 对象'); const p2 = Promise.resolve('第二个 Promise 对象'); const p3 = Promise.reject('第三个 Promise 对象'); Promise.all([p1, p2, p3]) .then((results) => { console.log(results); // [ '第一个 Promise 对象', '第二个 Promise 对象', '第三个 Promise 对象' ] }) .catch((error) => { console.log(error); // 第三个 Promise 对象 });</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Promise.race()</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Promise.race() 方法可以同时处理多个 Promise 对象,等待其中任何一个 Promise 对象完成后,就将它的结果返回。如果其中任何一个 Promise 对象失败了,则整个 Promise.race() 也会失败。比如:</p><pre class="brush:as3;toolbar:false">const p1 = new Promise((resolve) => { setTimeout(() => { resolve('第一个 Promise 对象'); }, 1000); }); const p2 = new Promise((resolve) => { setTimeout(() => { resolve('第二个 Promise 对象'); }, 500); }); Promise.race([p1, p2]) .then((result) => { console.log(result); // 第二个 Promise 对象 }) .catch((error) => { console.log(error); });</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">综上所述,ES6 Promise 提供了一种优雅和灵活的方式来处理异步编程,可以提高代码质量和开发效率。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p><br/></p>
<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">ES6(ECMAScript 2015)是 JavaScript 的一个版本,它在语言层面上新增了许多有用的特性,包括 Promise 和 Class 类两个重要的概念。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Promise</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Promise 是一种处理异步操作的机制,它可以避免回调地狱,使得异步代码更加易读和可维护。Promise 对象代表了一个异步操作的最终完成(或失败)以及其结果值的表示。Promise 对象有三个状态:pending(进行中)、fulfilled(已成功)和rejected(已失败)。使用 Promise 可以通过链式调用的方式处理异步操作的结果,从而避免回调地狱的问题。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Class 类</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">ES6 中新增了 Class 类的概念,它是一种用来创建对象的模板,提供了更加优雅和灵活的面向对象编程方式。Class 类可以看作是构造函数的一种语法糖,它包含了构造函数、原型和实例对象三部分。Class 类中可以定义构造函数、实例方法、静态方法和访问器等内容,而这些内容在传统的构造函数中需要通过 prototype 来定义。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Class 类与传统的构造函数相比,有以下优点:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">更加易读和易于维护:Class 类提供了更加优雅和灵活的面向对象编程方式,可以使代码更加清晰易懂,更易于维护。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">继承更加简单:Class 类提供了更加简单和易于理解的继承方式,可以通过 extends 关键字来实现继承。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">更加严谨和安全:Class 类的定义和使用都更加严谨和安全,可以减少一些常见的错误和问题。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">综上所述,Promise 和 Class 类是 ES6 中两个重要的概念,它们为 JavaScript 编程提供了更加优雅和灵活的方式,可以提高开发效率和代码质量。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p><br/></p>
<p>ES6(ECMAScript 2015)是 JavaScript 的一个版本,它在语言层面上新增了许多有用的特性,包括许多新的方法,这些方法为 JavaScript 编程提供了更多的便利。下面是 ES6 中新增的一些方法:</p><p><br/></p><p>Array.from():将类似数组的对象或可迭代对象转换为数组。</p><p>Array.of():创建一个包含任意数量参数的新数组。</p><p>Array.prototype.find():返回数组中满足指定条件的第一个元素。</p><p>Array.prototype.findIndex():返回数组中满足指定条件的第一个元素的下标。</p><p>Array.prototype.fill():用指定的值填充数组。</p><p>Array.prototype.copyWithin():将数组中指定的元素复制到指定位置,覆盖原有元素。</p><p>String.prototype.startsWith():判断一个字符串是否以指定字符串开头。</p><p>String.prototype.endsWith():判断一个字符串是否以指定字符串结尾。</p><p>String.prototype.includes():判断一个字符串是否包含指定字符串。</p><p>String.prototype.repeat():返回一个由指定字符串重复指定次数的新字符串。</p><p>String.prototype.padStart():用指定的字符串填充一个字符串,使其达到指定的长度。</p><p>String.prototype.padEnd():用指定的字符串填充一个字符串,使其达到指定的长度。</p><p>Object.assign():将一个或多个对象的属性拷贝到目标对象中。</p><p>Object.keys():返回一个包含所有可枚举属性名称的数组。</p><p>Object.values():返回一个包含所有可枚举属性值的数组。</p><p>Object.entries():返回一个包含所有可枚举属性键值对的数组。</p><p>这些方法为 JavaScript 编程提供了更多的便利,可以更加高效地处理各种数据和对象,从而提高了开发效率和代码质量。</p><p><br/></p>
<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">ES6(ECMAScript 2015)是 JavaScript 的一个版本,它在语言层面上新增了许多有用的特性,包括一些新的数据结构,这些数据结构提供了更好的性能和更多的功能。下面是 ES6 中新增的一些数据结构:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Map:一种有序的键值对集合,其中的键可以是任意类型,而不仅仅是字符串类型。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Set:一种无序的、不重复的值的集合,其中的元素可以是任意类型。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">WeakMap:一种只能使用对象作为键的 Map,键被弱引用,当键所对应的对象被垃圾回收时,该键也会自动被移除。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">WeakSet:一种只能存储对象的 Set,元素被弱引用,当元素所对应的对象被垃圾回收时,该元素也会自动被移除。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Symbol:一种原始数据类型,表示独一无二的值,主要用于对象属性的命名和保护。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">Promise:一种处理异步操作的机制,可以避免回调地狱,使得异步代码更加易读和可维护。</p><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "PingFang SC"; white-space: normal;">这些数据结构都是在<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue";"> ES6 </span>中新增的,它们的引入使得<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: "Helvetica Neue";"> JavaScript </span>的功能更加完善,可以更加高效地处理各种数据结构,从而提高了开发效率和代码质量。</p><p><br/></p>
<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">JavaScript 对象字面量是一种更加简洁、灵活的表达方式,可以方便地创建一个对象并为其赋值属性。对象字面量可以通过花括号({})来定义一个对象,其中可以包含多个属性及其对应的值,属性名和值之间使用冒号(:)分隔,每个属性之间使用逗号(,)分隔。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">下面是一个简单的示例:</p><pre class="brush:as3;toolbar:false">const person = { name: 'Tom', age: 18, gender: 'male' };</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">上面的代码创建了一个名为 person 的对象,并为其定义了三个属性:name、age 和 gender。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">除了基本属性值之外,对象字面量还支持在属性值中定义函数,这样就可以定义对象的方法。例如:</p><pre class="brush:as3;toolbar:false">const person = { name: 'Tom', age: 18, gender: 'male', sayHello: function() { console.log('Hello, my name is ' + this.name); } }; person.sayHello(); // 输出'Hello, my name is Tom'</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在上面的代码中,我们为对象 person 定义了一个方法 sayHello(),用来输出该对象的名字。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">除此之外,对象字面量还支持动态属性名,可以使用中括号([])和表达式来定义属性名。例如:</p><pre class="brush:as3;toolbar:false">const propName = 'name'; const person = { [propName]: 'Tom', age: 18, gender: 'male' };</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在上面的代码中,我们使用变量 propName 的值作为属性名,从而实现了动态属性名的功能。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">对象字面量是一种非常常用的表达方式,尤其在创建简单的对象或配置对象时非常方便。它的灵活性也使得它可以轻松地创建动态的对象,从而实现更加复杂的功能。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p><br/></p>
<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在 JavaScript 中,可以使用对象和数组解构语法来自动解析数组或对象中的值。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">数组解构</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">对于数组,可以使用方括号([])和变量名来进行解构。例如:</p><pre class="brush:as3;toolbar:false">const arr = [1, 2, 3]; const [a, b, c] = arr; console.log(a); // 输出1 console.log(b); // 输出2 console.log(c); // 输出3</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在上面的例子中,[a, b, c] = arr 表示将数组 arr 中的前三个元素依次赋值给变量 a、b 和 c。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在解构时,还可以使用默认值和剩余操作符(...)来进行处理。例如:</p><pre class="brush:as3;toolbar:false">const arr = [1, 2]; const [a = 0, b = 0, ...rest] = arr; console.log(a); // 输出1 console.log(b); // 输出2 console.log(rest); // 输出[]</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在上面的例子中,a 和 b 都有默认值,如果对应的元素不存在,则会使用默认值。剩余操作符 ...rest 表示将剩余的元素都存储到 rest 变量中。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">对象解构</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">对于对象,可以使用花括号({})和变量名来进行解构。例如:</p><pre class="brush:as3;toolbar:false">const obj = { name: 'Tom', age: 18 }; const { name, age } = obj; console.log(name); // 输出'Tom' console.log(age); // 输出18</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在上面的例子中,{ name, age } = obj 表示将对象 obj 中的属性 name 和 age 分别赋值给变量 name 和 age。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">同样的,对象解构也支持默认值和剩余操作符。例如:</p><pre class="brush:as3;toolbar:false">const obj = { name: 'Tom' }; const { name, age = 18, ...rest } = obj; console.log(name); // 输出'Tom' console.log(age); // 输出18 console.log(rest); // 输出{}</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在上面的例子中,age 有默认值,如果 obj 中没有对应的属性,则会使用默认值。剩余操作符 ...rest 表示将剩余的属性都存储到 rest 变量中。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "PingFang SC"; white-space: normal;">使用解构语法可以很方便地自动解析数组或对象中的值,使代码更加简洁易读。</p><p><br/></p>
<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">箭头函数是ES6引入的新特性,它是一种更加简洁的函数定义方式,相比于传统函数,具有以下特点:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">箭头函数使用箭头(=>)来定义,语法更加简洁。例如:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">// 传统函数定义方式</p><pre class="brush:as3;toolbar:false">function add(x, y) { return x + y; } // 箭头函数定义方式 const add = (x, y) => x + y;</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">箭头函数的this指向固定,不会因为调用方式的不同而改变。箭头函数的this指向定义时所在的上下文,而不是执行时的上下文。例如:</p><pre class="brush:as3;toolbar:false">const obj = { name: 'Tom', getName: function() { // 传统函数定义方式 const self = this; return function() { console.log(self.name); // 输出'Tom' }; }, getNameArrow: function() { // 箭头函数定义方式 return () => { console.log(this.name); // 输出'Tom' }; } }; const getName = obj.getName(); const getNameArrow = obj.getNameArrow(); getName(); getNameArrow();</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">箭头函数不绑定arguments对象,但可以使用剩余参数语法来获取传入的所有参数。例如:</p><pre class="brush:as3;toolbar:false">function test() { console.log(arguments); // 输出[1, 2, 3] } test(1, 2, 3); const testArrow = (...args) => { console.log(args); // 输出[1, 2, 3] }; testArrow(1, 2, 3);</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">箭头函数在以下场景中应用广泛:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">用于回调函数,例如数组的map、reduce、filter等方法。</p><pre class="brush:as3;toolbar:false">const numbers = [1, 2, 3, 4, 5]; const doubled = numbers.map(x => x * 2); console.log(doubled); // 输出[2, 4, 6, 8, 10]</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">用于简化代码,例如事件监听器、定时器等。</p><pre class="brush:as3;toolbar:false">// 传统函数定义方式 setTimeout(function() { console.log('Hello, world!'); }, 1000); // 箭头函数定义方式 setTimeout(() => { console.log('Hello, world!'); }, 1000);</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">用于处理this指向问题,例如在React中使用箭头函数定义组件方法。</p><pre class="brush:as3;toolbar:false">class App extends React.Component { handleClick = () => { console.log('Button clicked!'); } render() { return <button onClick={this.handleClick}>Click me!</button>; } }</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">综上所述,箭头函数是一种更加简洁、易于使用的函数定义方式,可以大大提高代码的可读性和可维护性。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p>