2020-09-25【es2020】

ECMS SCRIPT

  • ECMAScript 协会组成(1998 ecmascript 2.0)
  • ECMAScript 发展历史

?? 空值运算【有默认值】


//  ||  无法正确返回 false /  undefined / null/ "" / 0


let  fun = function(a){
   return a = a || "hello"
}

fun(0) //"hello"
fun(null) // "hello"
fun(false) // "hello"
fun("") // "hello"
fun(1)// 1
fun(true) // true

//  false /  undefined / null  / ""/ 0 空值处理
//  浏览器vm 测试报错
let  foo = function(a){
   return a = a ?? "hello"
}

链式调用

//
let obj ={};
obj.a //  a    undefined
obj.a.b //  undefined.b  浏览器解析代码报错
obj.a?.b  //   阻断报错

class

  • 类的主要目的之一是将代码包含到更可重用的模块中
class Message {
 #message = "Howdy" //  private  cy变量
 getMessage(){
  return this.#message  //  私有变量只能通过get/set 方法获取
}
 
}

const greeting = new Message()
let getMessage = greeting.getMessage()
console.log(getMessage);  // Howdy
console.log(greeting.#message) // Private name #message is not defined

Promise.allSettled().then() == Promise 更新

// Promise.all;
// 使用场景:单线程特性,一个失效,所有失效
//  一个失效,其他的不会失效

const p1 = new Promise((res, rej) => setTimeout(res, 1000));
const p2 = new Promise((res, rej) => setTimeout(rej, 1000));
Promise.allSettled([p1, p2]).then(data => console.log(data));

// [
//   Object { status: "fulfilled", value: undefined},
//   Object { status: "rejected", reason: undefined}
// ]

可选的链接操作

  • 测试报错
  • 只允许在值为null或未定义时使用默认值。
let person = {};
console.log(person.profile.name ?? "Anonymous"); // person.profile is undefined
console.log(person?.profile?.name ?? "Anonymous");
console.log(person?.profile?.age ?? 18);

BigInt 数据类型

  • Number.MAX_SAFE_INTEGER 极大数
const max = Number.MAX_SAFE_INTEGER;
console.log(max); // 9007199254740991 超过这个范围,计算溢出
const bigNum = 100000000000000000000000000000n;
console.log(bigNum * 2n); // 200000000000000000000000000000n

async / await在需要时动态导入依赖项

  • await import(’./math.js’)

//  个人建议: 根据需求,查询es2020 api  累积学习

 const doMath = async (num1, num2) => {
  if (num1 && num2) {
    const math = await import('./math.js');
    console.log(math.add(5, 10));
  };
};
doMath(4, 2);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值