ECMAScript 2020
Bersyon ng JavaScript
Ang lumang bersyon ng JS ay pinangalanan ng numero: ES5 (2009) at ES6 (2015).
mula 2016, ang bersyon ay pinangalanan ayon sa taon: ECMAScript 2016, 2017, 2018, 2019, ...
Bagong katangian sa ES2020:
- BigInt
- Metodo ng string na matchAll()
- Operator ng pagkakasampalataya ng walang halaga (??)
- Operator ng optional chaining (?.)
- Operator ng logic AND sa pagtatalaga (&&=)
- Operator ng logic OR sa pagtatalaga (||=)
- Operator ng pagkakasampalataya ng walang halaga (??=)
- Promise.allSettled()
- Dinamikong pag-anggap
Warning
Ang mga katangian na ito ay napakabagong lamang.
Ang mas lumang browser ay maaaring kailangan ng alternative code (Polyfill).
BigInt in JavaScript
Ang variable ng BigInt sa JavaScript ay ginagamit para sa pag-iimbak ng malalaking integer na hindi maaaring maisipagmataas ng pangkaraniwang JavaScript number.
Ang integer sa JavaScript ay maaring maayos lamang hanggang 15 na numero.
instance ng Integer
let x = 999999999999999; let y = 9999999999999999; // napakalaki
instance ng BigInt
let x = 9999999999999999; let y = 9999999999999999n;
Para sa paglikha ng BigInt, i-set sa n
magdagdag sa huli ng integer o tumawag sa BigInt():
Example
let x = 1234567890123456789012345n; let y = BigInt(1234567890123456789012345);
Ang JavaScript type ng BigInt ay "bigint":
Example
let x = BigInt(999999999999999); let type = typeof x;
mula 2020 Setyembre 9, lahat ng modernong browser ay sumusuporta sa BigInt:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 67 | Edge 79 | Firefox 68 | Safari 14 | Opera 54 |
May 2018 | January 2020 | July 2019 | 2020 年 9 月 | June 2018 |
JavaScript 字符串方法 matchAll()
Before ES2020, there was no string method available to search for all occurrences of a string in a string.
Example
const iterator = text.matchAll("Cats");
If the parameter is a regular expression, you must set the global flag (g
) or it will throw a TypeError.
Example
const iterator = text.matchAll(/Cats/g);
If you want to perform a case-insensitive search, you must set the case-insensitive flag (i
)
Example
const iterator = text.matchAll(/Cats/gi);
Hint:ES2021 The string method replaceAll() was introduced.
Nullish Coalescing Operator (Nullish Coalescing Operator) (?? Operator)
If the first parameter is not a null value (null
or undefined
),then ??
Operator returns the first parameter.
otherwise return the second.
Example
let name = null; let text = "missing"; let result = name ?? text;
Since March 2020, all modern browsers have supported nullish operators:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 80 | Edge 80 | Firefox 72 | Safari 13.1 | Opera 67 |
February 2020 | February 2020 | January 2020 | 2020 年 3 月 | 2020 年 3 月 |
Optional Chaining Operator (Optional Chaining Operator) (?. Operator)
If the object is undefined
or null
thenOptional Chaining OperatorReturns undefined
(instead of throwing an error).
Example
const car = {type:"Fiat", model:"500", color:"white"}; let name = car?.name;
Since March 2020, all modern browsers have supported ?.=
运算符:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 80 | Edge 80 | Firefox 74 | Safari 13.1 | Opera 67 |
February 2020 | February 2020 | 2020 年 3 月 | 2020 年 3 月 | 2020 年 3 月 |
逻辑 AND 赋值运算符(&&= 运算符)
Logic AND Assignment Operatorused between two values.
If the first value is true
,则分配第二个值。
Logic AND Assignment Example
let x = 100; x &&= 5;
自 2020 年 9 月以来,所有现代浏览器都支持 &&=
运算符:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 85 | Edge 85 | Firefox 79 | Safari 14 | Opera 71 |
2020 年 8 月 | 2020 年 8 月 | 2020 年 3 月 | 2020 年 9 月 | 2020 年 9 月 |
Logic OR Assignment Operator (||= Operator)
Logic OR Assignment Operatorused between two values.
If the first value is false
,则分配第二个值。
Logic OR Assignment Example
let x = 10; x ||= 5;
自 2020 年 9 月以来,所有现代浏览器都支持 ||=
运算符:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 85 | Edge 85 | Firefox 79 | Safari 14 | Opera 71 |
2020 年 8 月 | 2020 年 8 月 | 2020 年 3 月 | 2020 年 9 月 | 2020 年 9 月 |
空值合并赋值运算符(??= 运算符)
空值合并赋值运算符(Nullish Coalescing Assignment Operator)用于两个值之间。
如果第一个值 undefined
或为 null
,则分配第二个值。
空值合并赋值实例
let x = 10; x ??= 5;
自 2020 年 9 月以来,所有现代浏览器都支持 ??=
运算符:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 85 | Edge 85 | Firefox 79 | Safari 14 | Opera 71 |
2020 年 8 月 | 2020 年 8 月 | 2020 年 3 月 | 2020 年 9 月 | 2020 年 9 月 |