es2021新特性


1、首先替換  String.prototype.replace

效果 

 const str = 'I like frontend. I like JavaScript.';

const newStr = str.replace('like', 'love');
newStr="I love frontend. I like JavaScript."

單一替換第一個匹配項   然后全部替換怎么破?

const newStr = str.replaceAll('like', 'love');

newStr="I love frontend. I love JavaScript."

 

2、Promise.any(promises).then(
  (first) => {
    // 任何一個 Promise 完成了
  },
  (error) => {
    // 所有的 Promise 都拒絕了
  }
);

 

3、?? 雙問號神器

假設 lrealCount=null   或者undefined 的時候就給一個默認值

如果 lrealCount =0時   let count = realCount || '無法獲取'      那么此時content就等於“無法獲取”了

此時無法忍受了   那么怎么讓其等於0呢

let count = realCount ?? '無法獲取'       去試試唄 哈哈

 

4、數字分隔符 _

let x = 2_3333_3333      x=233333333

 

5、Intl.ListFormat

const list = ['Apple', 'Orange', 'Banana']
new Intl.ListFormat('en-GB', { style: 'long', type: 'conjunction' }).format(list);
// "Apple, Orange and Banana"
new Intl.ListFormat('zh-cn', { style: 'short', type: 'conjunction' }).format(list);
// 會根據語言來返回相應的格式化操作
// "Apple、Orange和Banana"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM