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"
原文:https://www.cnblogs.com/cq1715584439/p/13439702.html