index.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. <script src="./src/index.js" type="module"></script>
  9. </head>
  10. <body>
  11. <button id="btn"> hello </button>
  12. <script>
  13. /**
  14. * 1、es11私有属性
  15. */
  16. class Person{
  17. name
  18. #age //私有
  19. #weight
  20. constructor(name,age,weight){
  21. this.name = name
  22. this.#age = age
  23. this.#weight = weight
  24. }
  25. intro(){
  26. console.log(this.#age)
  27. }
  28. }
  29. const girl = new Person('lily',18,'45kg')
  30. console.log(girl)
  31. //console.log(girl.#age) //私有 无法访问
  32. girl.intro() //18
  33. /**
  34. * 2、Promise.allSettled
  35. */
  36. const p1 = new Promise((resolve,reject)=>{
  37. setTimeout(()=>{
  38. resolve('goods1')
  39. },1000)
  40. })
  41. const p2 = new Promise((resolve,reject)=>{
  42. setTimeout(()=>{
  43. resolve('goods2')
  44. },1000)
  45. })
  46. const result1 = Promise.allSettled([p1,p2]) //无论reject与否,都会返回结果及状态
  47. console.log(result1)
  48. // Promise.all()
  49. const result2 = Promise.all([p1,p2]) //当有一个reject时,则终断程序,全部成功时只返回结果
  50. console.log(result2)
  51. /**
  52. * 3、String.prototype.matchAll 正则批量匹配
  53. */
  54. let str = `
  55. <ul>
  56. <li>
  57. <a>123</a>
  58. <p>1</p>
  59. </li>
  60. <li>
  61. <a>5345</a>
  62. <p>2</p>
  63. </li>
  64. </ul>`
  65. const reg = /<li>.*?<a>(.*?)<\/a>.*?<p>(.*?)<\/p>/sg
  66. const result4 = str.matchAll(reg)
  67. console.log(result4) //可迭代对象
  68. for(let v of result4){
  69. console.log(v)
  70. }
  71. let r4 = [...result4]
  72. console.log(r4)
  73. /**
  74. * 4、可选链操作符 ?.
  75. */
  76. function main(config){
  77. const dbHost = config?.db?.host //免去层层判断
  78. //等价于cosnt dbHost = config && config.db && config.db.host
  79. }
  80. main({
  81. db:{
  82. host:'23443243243',
  83. usernmae:'root'
  84. },
  85. catch:{
  86. host:'34343',
  87. username:'admin'
  88. }
  89. })
  90. /**
  91. * 5、动态import
  92. */
  93. //示例:./src/index.js
  94. /**
  95. * 6、BigInt类型
  96. * 应用于大数值运算
  97. */
  98. let n = 521n
  99. console.log(n,typeof(n))
  100. console.log(BigInt(123)) //123n 不支持浮点
  101. let max = Number.MAX_SAFE_INTEGER //最大安全整数
  102. console.log(max+2) //无法再加
  103. console.log(BigInt(max)+BigInt(2))
  104. /**
  105. * 7、globalThis 全局 浏览器 node
  106. */
  107. console.log(globalThis) //widnow global
  108. </script>
  109. </body>
  110. </html>