Content
6 Lessons • 0 Quizzes • 0 Challenges
简介
- 1
简介
- 2
关于我
JavaScript入门
- 1
简介与变量
- 2
数据类型
- 3
函数
- 4
测验
ES6
- 1
var、let 和 const 以及暂时性死区
- 2
测验
- 3
箭头函数
- 4
测验
- 5
默认函数参数
- 6
测验
- 7
模板字符串
- 8
测验
- 9
额外的字符串方法
- 10
测验
- 11
解构
- 12
测验
- 13
可迭代对象与循环
- 14
测验
- 15
数组优化
- 16
测验
- 17
展开运算符和剩余参数
- 18
测验
- 19
对象字面量升级
- 20
测验
- 21
符号
- 22
测验
- 23
类
- 24
测验
- 25
Promise
- 26
测验
- 27
生成器
- 28
测验
- 29
代理
- 30
测验
- 31
集合、弱集合、映射和弱映射
- 32
测验
从 ES2016 到 ES2022
- 1
ES2016 中的新特性
- 2
测验
- 3
ES2017:字符串填充、Object.entries() 及其他特性
- 4
测验
- 5
ES2017:Async 和 Await
- 6
测验
- 7
ES2018:异步迭代及其他特性
- 8
测验
- 9
ES2019 新特性
- 10
测验
- 11
ES2020 新特性
- 12
测验
- 13
ES2021(也称为 ECMAScript 2021)引入了多项新特性,增强了 JavaScript 的表达能力和开发效率。以下是 ES2021 的主要更新内容: 1. **逻辑赋值操作符(Logical Assignment Operators)** 新增了三种逻辑赋值操作符,用于在条件为真或假时进行赋值: - `&&=`:当左侧表达式为真时,将右侧值赋给左侧。 - `||=`:当左侧表达式为假时,将右侧值赋给左侧。 - `??=`:当左侧表达式为 null 或 undefined 时,将右侧值赋给左侧。 示例: ```js let a = 5; a &&= 10; // a 仍为 5(因为 5 为真,但不会改变) a ||= 20; // a 变为 20(因为 a 为真,不执行赋值) a ??= 30; // a 仍为 5(因为 a 不是 null 或 undefined) ``` 2. **可选链操作符(Optional Chaining)的增强** 支持在对象属性访问和方法调用中使用可选链,避免因深层嵌套导致的运行时错误。现在可以更灵活地处理可能为 null 或 undefined 的路径。 示例: ```js const user = { profile: { name: "Alice" } }; console.log(user.profile?.name); // "Alice" console.log(user.profile?.address?.city); // undefined(不会报错) ``` 3. **空值合并操作符(Nullish Coalescing Operator)** 使用 `??` 操作符来判断一个值是否为 `null` 或 `undefined`,如果是,则返回默认值。与 `||` 不同,它只在值为 `null` 或 `undefined` 时才生效,不适用于其他“假值”如 `0`、`""`、`false`。 示例: ```js const count = 0; const result = count ?? 10; // 0(因为 0 不是 null 或 undefined) const value = null ?? "default"; // "default" ``` 4. **Promise.any() 方法** 接收一个 Promise 数组,返回第一个成功(fulfilled)的 Promise。如果所有 Promise 都失败,则抛出一个 `AggregateError` 错误,包含所有失败原因。 示例: ```js const promise1 = Promise.reject("Error 1"); const promise2 = Promise.resolve("Success"); Promise.any([promise1, promise2]) .then(result => console.log(result)) // "Success" .catch(err => console.error(err)); // 不会进入这里 ``` 5. **Array.prototype.at() 方法** 提供一种安全的方式来访问数组元素,支持负索引(从末尾开始计数),避免因索引越界导致的错误。 示例: ```js const arr = [1, 2, 3]; console.log(arr.at(1)); // 2 console.log(arr.at(-1)); // 3(最后一个元素) ``` 6. **String.prototype.replaceAll() 方法** 提供一种更简洁的方式替换字符串中的所有匹配项,而不是仅替换第一个。 示例: ```js const str = "hello world hello"; console.log(str.replaceAll("hello", "hi")); // "hi world hi" ``` 7. **BigInt 支持的改进** BigInt 现在可以在 `for...of` 循环中使用,并且支持更多内置方法,例如 `BigInt.prototype.toString()` 和 `BigInt.prototype.valueOf()`。 8. **全局对象的统一访问** 在模块环境中,可以通过 `globalThis` 统一访问全局对象(如 `window`、`global`、`self`),避免环境差异带来的问题。 这些新特性使 JavaScript 更加健壮、易读和高效,尤其在处理复杂数据结构和异步操作时表现突出。
- 14
测验
- 15
ES2022 新特性
- 16
测验
TypeScript 入门
- 1
基本类型
- 2
接口、类与更多内容
- 3
测验
- 4
结论
附录
- 1
设置
- 2
结论
Certificate of Completion
Showcase your accomplishment by sharing your certificate of completion.
Course Author:Anonymous User