12_Symbol.html 632 B

123456789101112131415161718192021222324252627
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // Symbol
  11. let username = Symbol();
  12. let obj = {
  13. age:18,
  14. school:"xxx大学",
  15. // username:"李四"
  16. }
  17. obj[username] = "张三"
  18. // obj.username = "张三"
  19. console.log(obj);
  20. // 获取Symbol属性值
  21. console.log(obj[username]);
  22. // Symbol 一般用作于对象的属性名 防止属性名冲突
  23. </script>
  24. </body>
  25. </html>