index.html 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. /**
  12. * 1、replaceAll
  13. */
  14. const str = 'hello world';
  15. str.replaceAll('l', ''); // "heo word"
  16. /**
  17. * 2、Promise.any
  18. * 若有成功,返回其中一个成功的,若都不成功,则返回All promises were rejected
  19. * es11中Promise.allSettled 无论成功失败都返回,并有状态
  20. */
  21. const promise1 = new Promise((resolve, reject) => resolve('我是成功的Promise_1'));
  22. const promise2 = new Promise((resolve, reject) => resolve('我是失败的Promise_2'));
  23. const promiseList = [promise1, promise2];
  24. Promise.any(promiseList)
  25. .then(values=>{
  26. console.log(values); //AggregateError: All promises were rejected
  27. }).catch(e=>{
  28. console.log(e);
  29. });
  30. /**
  31. * 3、WeakRefs
  32. * 使用WeakRefs的Class类创建对对象的弱引用(对对象的弱引用是指当该对象应该被GC回收时不会阻止GC的回收行为)
  33. */
  34. /**
  35. * 4、运算逻辑符和赋值表达式
  36. */
  37. // a ||= b
  38. // //等价于
  39. // a = a || (a = b) //取第一个真值,都是假时返回最后一个值
  40. // a &&= b
  41. // //等价于
  42. // a = a && (a = b) //第一个真值,返回第二个,第一个假值,直接返回假值,都是真时,返回最后一个
  43. // a ??= b
  44. // //等价于
  45. // a = a ?? (a = b)
  46. let a = 1
  47. let b = 2
  48. console.log(a ?? (a=b))
  49. /**
  50. * 5、数字分割符
  51. */
  52. const money = 1_000_000_000;
  53. //等价于
  54. const money1 = 1000000000;
  55. console.log(1_000_000_000 === 1000000000); // true
  56. </script>
  57. </body>
  58. </html>