index.html 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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>rest</title>
  8. </head>
  9. <body>
  10. <script>
  11. /**
  12. * 值唯一
  13. * 不能运算
  14. * 不能使用for in,可以使用reflect.ownkeys
  15. */
  16. //创建symbol
  17. let s = Symbol()
  18. console.log(s,typeof s) //Symbol() "symbol"
  19. let s1 = Symbol('chuck') //描述字符串
  20. let s2 = Symbol('chuck') //描述字符串
  21. console.log(s1) //Symbol(chuck)
  22. console.log(s1 === s2) //false
  23. let s3 = Symbol.for('chuck') //函数对象
  24. let s4 = Symbol.for('chuck')
  25. console.log(s3,typeof s3) //Symbol(chuck) "symbol"
  26. console.log(s3 === s4) // true
  27. //不能运算 否则报错
  28. //let result = s + 100;
  29. //数据类型
  30. //USONB you are so niubility
  31. /**
  32. * undefined
  33. * string
  34. * object
  35. * null
  36. * symbol
  37. */
  38. //symbol 使用场景
  39. //给对象添加方法 up down -- 可以很安全的加入方法
  40. let game = { }
  41. let methods = {
  42. up: Symbol(),
  43. down: Symbol()
  44. }
  45. game[methods.up] = function(){}
  46. game[methods.down] = function(){}
  47. //2
  48. let cc = {
  49. name : 'chuck',
  50. [Symbol('say')]:function(){}
  51. }
  52. // symbol 内置属性 共11个
  53. /**
  54. * hasInstance
  55. * match
  56. * replace
  57. * isConcatSpreadable
  58. */
  59. class Person{
  60. //自动执行
  61. static [Symbol.hasInstance](param){
  62. console.log(param) //{d:2}
  63. console.log('检测')
  64. }
  65. }
  66. let o = {d:2}
  67. console.log(o instanceof Person)
  68. const arr = [1,2,3]
  69. const arr1 = [4,5,6]
  70. arr1[Symbol.isConcatSpreadable] = false;//不可展开 合并
  71. console.log(arr.concat(arr1))
  72. </script>
  73. </body>
  74. </html>