09.对象2.html 885 B

12345678910111213141516171819202122232425262728293031323334
  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. <!--
  11. 问题:什么时候必须使用['属性名']的方式?
  12. 1.属性名包含特殊字符:- 空格
  13. 2.属性名不确定
  14. -->
  15. <script>
  16. var p = {};
  17. //1.给p对象添加一个属性:content-type:text/json
  18. // p.content-type = 'text/json';//报错,不能这样使用
  19. p['content-type'] = 'text/json';
  20. console.log(p['content-type']);
  21. //2.属性名不确定
  22. var propName = 'myAge';
  23. var value = 18;
  24. // p.propName = value;//不能用
  25. p[propName] = value;
  26. console.log(p[propName]);
  27. </script>
  28. </body>
  29. </html>