2020-05-14 21:41:53 +08:00
|
|
|
|
|
|
|
|
|
## 对象的基本操作
|
|
|
|
|
|
|
|
|
|
### 创建对象
|
|
|
|
|
|
|
|
|
|
使用new关键字调用的函数,是构造函数constructor。**构造函数是专门用来创建对象的函数**。
|
|
|
|
|
|
|
|
|
|
例如:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var obj = new Object();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
记住,使用`typeof`检查一个对象时,会返回`object`。
|
|
|
|
|
|
|
|
|
|
关于常见对象的更多方式,可以看上一篇文章《对象的创建&构造函数》。
|
|
|
|
|
|
|
|
|
|
### 向对象中添加属性
|
|
|
|
|
|
|
|
|
|
在对象中保存的值称为属性。
|
|
|
|
|
|
|
|
|
|
向对象添加属性的语法:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
对象.属性名 = 属性值;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var obj = new Object();
|
|
|
|
|
|
|
|
|
|
//向obj中添加一个name属性
|
|
|
|
|
obj.name = "孙悟空";
|
|
|
|
|
|
|
|
|
|
//向obj中添加一个gender属性
|
|
|
|
|
obj.gender = "男";
|
|
|
|
|
|
|
|
|
|
//向obj中添加一个age属性
|
|
|
|
|
obj.age = 18;
|
|
|
|
|
|
|
|
|
|
console.log(JSON.stringify(obj)); // 将 obj 以字符串的形式打印出来
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
打印结果:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
{
|
|
|
|
|
"name":"孙悟空",
|
|
|
|
|
"gender":"男",
|
|
|
|
|
"age":18
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 获取对象中的属性
|
|
|
|
|
|
|
|
|
|
**方式1**:
|
|
|
|
|
|
|
|
|
|
语法:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
对象.属性名
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
如果获取对象中没有的属性,不会报错而是返回`undefined`。
|
|
|
|
|
|
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var obj = new Object();
|
|
|
|
|
|
|
|
|
|
//向obj中添加一个name属性
|
|
|
|
|
obj.name = "孙悟空";
|
|
|
|
|
|
|
|
|
|
//向obj中添加一个gender属性
|
|
|
|
|
obj.gender = "男";
|
|
|
|
|
|
|
|
|
|
//向obj中添加一个age属性
|
|
|
|
|
obj.age = 18;
|
|
|
|
|
|
|
|
|
|
// 获取对象中的属性,并打印出来
|
|
|
|
|
console.log(obj.gender); // 打印结果:男
|
|
|
|
|
console.log(obj.color); // 打印结果:undefined
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**方式2**:可以使用`[]`这种形式去操作属性
|
|
|
|
|
|
|
|
|
|
对象的属性名不强制要求遵守标识符的规范,不过我们尽量要按照标识符的规范去做。
|
|
|
|
|
|
|
|
|
|
但如果确实要使用特殊的属性名,就不能采用`.`的方式来操作对象的属性。比如说,`123`这种属性名,如果我们直接写成`obj.123 = 789`来操作属性,是会报错的。那怎么办呢?办法如下:
|
|
|
|
|
|
|
|
|
|
语法格式如下:(读取时,也是采用这种方式)
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
// 注意,括号里的属性名,必须要加引号
|
|
|
|
|
对象['属性名'] = 属性值
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
上面这种语法格式,举例如下:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
obj['123'] = 789;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**重要**:使用`[]`这种形式去操作属性,更加的灵活,因为,我们可以在`[]`中直接传递一个**变量**。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 修改对象的属性值
|
|
|
|
|
|
|
|
|
|
语法:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
对象.属性名 = 新值
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
obj.name = "tom";
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 删除对象的属性
|
|
|
|
|
|
|
|
|
|
语法:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
delete obj.name;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### in 运算符
|
|
|
|
|
|
|
|
|
|
通过该运算符可以检查一个对象中是否含有指定的属性。如果有则返回true,没有则返回false。
|
|
|
|
|
|
|
|
|
|
语法:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
"属性名" in 对象
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
//检查obj中是否含有name属性
|
|
|
|
|
console.log("name" in obj);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
我们平时使用的对象不一定是自己创建的,可能是别人提供的,这个时候,in 运算符可以派上用场。
|
|
|
|
|
|
2020-05-23 17:44:13 +08:00
|
|
|
|
## 遍历对象:for in
|
2020-05-14 21:41:53 +08:00
|
|
|
|
|
|
|
|
|
语法:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
for (var 变量 in 对象名) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
解释:对象中有几个属性,循环体就会执行几次。每次执行时,会将对象中的**每个属性的 属性名 赋值给变量**。
|
|
|
|
|
|
|
|
|
|
语法举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
for (var key in obj) {
|
2020-05-23 17:44:13 +08:00
|
|
|
|
console.log(key); // 这里的 key 是:对象属性的键(也就是属性名)
|
|
|
|
|
console.log(obj[key]); // 这里的 obj[key] 是:对象属性的值(也就是属性值)
|
2020-05-14 21:41:53 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html>
|
|
|
|
|
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<title></title>
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
var obj = {
|
|
|
|
|
name: "smyhvae",
|
|
|
|
|
age: 26,
|
|
|
|
|
gender: "男",
|
|
|
|
|
address: "shenzhen",
|
|
|
|
|
sayHi: function() {
|
|
|
|
|
console.log(this.name);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 遍历对象中的属性
|
|
|
|
|
for (var key in obj) {
|
|
|
|
|
console.log("属性名:" + key);
|
|
|
|
|
console.log("属性值:" + obj[key]); // 注意,因为这里的属性名 keu 是变量,所以,如果想获取属性值,不能写成 obj.key,而是要写成 obj[key]
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
</head>
|
|
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
</body>
|
|
|
|
|
|
|
|
|
|
</html>
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
打印结果:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
属性名:name
|
|
|
|
|
属性值:smyhvae
|
|
|
|
|
|
|
|
|
|
属性名:age
|
|
|
|
|
属性值:26
|
|
|
|
|
|
|
|
|
|
属性名:gender
|
|
|
|
|
属性值:男
|
|
|
|
|
|
|
|
|
|
属性名:address
|
|
|
|
|
属性值:shenzhen
|
|
|
|
|
|
|
|
|
|
属性名:sayHi
|
|
|
|
|
属性值:function() {
|
|
|
|
|
console.log(this.name);
|
|
|
|
|
}
|
|
|
|
|
```
|