ECMS SCRIPT
ECMAScript 协会组成(1998 ecmascript 2.0) ECMAScript 发展历史
?? 空值运算【有默认值】
let fun = function ( a) {
return a = a || "hello"
}
fun ( 0 )
fun ( null )
fun ( false )
fun ( "" )
fun ( 1 )
fun ( true )
let foo = function ( a) {
return a = a ? ? "hello"
}
链式调用
let obj = { } ;
obj. a
obj. a. b
obj. a? . b
class
class Message {
#message = "Howdy"
getMessage ( ) {
return this . #message
}
}
const greeting = new Message ( )
let getMessage = greeting. getMessage ( )
console. log ( getMessage) ;
console. log ( greeting. #message)
Promise.allSettled().then() == Promise 更新
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) ) ;
可选的链接操作
测试报错 只允许在值为null或未定义时使用默认值。
let person = { } ;
console. log ( person. profile. name ? ? "Anonymous" ) ;
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) ;
const bigNum = 100000000000000000000000000000 n;
console. log ( bigNum * 2 n) ;
async / await在需要时动态导入依赖项
await import(’./math.js’)
const doMath = async ( num1, num2) => {
if ( num1 && num2) {
const math = await import ( './math.js' ) ;
console. log ( math. add ( 5 , 10 ) ) ;
} ;
} ;
doMath ( 4 , 2 ) ;