12345678910111213141516171819202122232425262728293031323334 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <!--
- 问题:什么时候必须使用['属性名']的方式?
- 1.属性名包含特殊字符:- 空格
- 2.属性名不确定
- -->
- <script>
- var p = {};
- //1.给p对象添加一个属性:content-type:text/json
- // p.content-type = 'text/json';//报错,不能这样使用
- p['content-type'] = 'text/json';
- console.log(p['content-type']);
- //2.属性名不确定
- var propName = 'myAge';
- var value = 18;
- // p.propName = value;//不能用
- p[propName] = value;
- console.log(p[propName]);
- </script>
- </body>
- </html>
|