对象扩展
Rio 2023-01-16
js
说明
属性描述符、对象方法笔记总结
#
# 属性描述符
属性描述符可以精准的添加或修改对象的属性;
属性描述符需要使用Object.defineProperty
来对属性进行添加或者修改;
defineProperty为对象设置属性后,该属性的描述符writable、configurable以及enumberable默认为false
- writable:表示能否修改属性的值,也就是说该属性是可写的还是只读的,默认为true(可写)
- enumerable:表示改属性是否可遍历,默认为true(可遍历)
- configurable:表示能否通过 delete 删除属性、能否修改属性的特性,或者将属性修改为访问器属性,默认为true(可配置)
let obj = { brand: '华为', price: 1999 };
Object.defineProperty(obj, 'id', { value: 1 })
Object.defineProperty(obj, 'price', { configurable: false })
console.log(Object.keys(obj)); // ['brand','price']
for (let k in obj) {
console.log(k); // brand price
}
obj.price = 999;
delete obj['price']
console.log(obj); // { brand: '华为', price: 1999,id: 1}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
可接收三个参数:
- obj定义属性的对象;
- prop要定义或修改的属性的名称或Symbol;
- descriptor要定义或修改的属性描述符;
属性描述符的类型有两种:
- 数据属性描述符 ;
- 存取属性描述符;
# 数据属性描述符
let obj = {
name:"why",
age: 18
}
Object.defineProperty(obj,"name",{
configurable: false, // name属性特性不可修改
enumerable: false, // name属性不可枚举
writable: false, // 不能被赋值运算符改变
})
delete obj.name
console.log(obj); // {name: 'rio', age: 20}
console.log(Object.keys(obj)); // ['age']
obj.name = 'RIO'
console.log(obj); // {name: 'rio', age: 20}
------------------------------------------------------
let obj = {
name:"rio",
age: 20
}
Object.defineProperty(obj,"name",{
value: 'RIO', // 读取属性时会返回该值,修改属性时,会对其进行修改;
})
console.log(obj); // {name: 'RIO', age: 20}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 遍历对象
for ...in... 可以把原型对象身上的属性遍历出来
Object.keys() 不能把原型对象身上的属性遍历出来
let obj = {
a: 1,
b: 'qwe',
c:[],
}
Object.prototype.d = "qwe"
for(let o in obj){
console.log(o); // a b c d
}
console.log(Object.keys(obj)); // ['a','b','c']
console.log(obj.hasOwnProperty('a')); // true
console.log(obj.hasOwnProperty('d')); // false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 存取属性描述符
set 和 get
let obj = {
name: "rio"
}
var _name = ""
Object.defineProperty(obj,'name',{
configurable: true,
enumerable: false,
set:function(value){
console.log("set " + value);
_name = value
},
get:function(){
console.log("get!");
return _name
}
})
obj.name = 'q' // set q
obj.name = 'w' // set q
console.log(obj.name); // get! w
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 多属性描述符
Object.defineProperties
var obj = {
name: 'rio',
age: 20,
no: 256
}
Object.defineProperties(obj,{
name:{
configurable:true
},
age:{
configurable: true
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 对象方法
# Object.getOwnPropertyDescriptor
获取对象属性描述符
var obj = {
name: 'rio',
age: 20,
no: 256
}
console.log(Object.getOwnPropertyDescriptor(obj,'name'));
console.log(Object.getOwnPropertyDescriptors(obj));
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# Object.preventExtensions
禁止对象扩展新属性给一个对象添加新的属性会失败(在严格模式下会报错);
# Object.seal
密封对象,不允许配置和删除属性
实际是调用preventExtensions
并且将现有属性的configurable:false
# Object.freeze
冻结对象,不允许修改现有属性:
实际上是调用seal 并且将属性的writable: false
# 字面量增强和解构
let name = 'rio'
let age = 20
let key = 'qwe'
let obj = {
name,
age,
[key]: "zxc"
}
console.log(obj); // {name: 'rio', age: 20, qwe: 'zxc'}
let {name:a,age:b=25,qwe:c,d=25} = obj
console.log(a,b,c,d); // rio 20 zxc 25
let {name:a,...b} = obj
console.log(a,b); // rio {age: 20, qwe: 'zxc'}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16