14对象的方法
14对象的方法
1.创建数组的方式
const obj = {nickname: '宠儿'};//字面量
const obj_a = new Object({ nikename:'上帝的宠儿'});//构造函数式
const obj_b = Object( {nikename: '耶和华的宠儿'});
2.Object.defineProperty/defineProperties
Object.defineProperty
给object定义私有属性
Object.defineProperties
同时定义多个私有属性(我们手动给对象添加的属性)
Object.defineProperty(
给哪个对象定义属性, ‘属性名是什么’, {属性值的配置})
属性值的配置:
- value: ‘属性值是什么’,
- writable: true/false, 是否可写 默认为false
- ennumerable: true/ fasle, 是否可枚举(遍历) 默认为false
- configurable: true/ false 是否可改配置 (锁) 默认为false
- configurable是一把关上就打不开的锁。
- 锁关上时,如果原来可写,能够改成不可写,如果原来不可写,不能改成可写
const a = { nickname: 'a' };
Object.defineProperty(a, 'age', {
value: 123
});
a.age = 456;
console.log(a);//{nickname: "a", age: 123}
const b = { nickname: '小明' };
Object.defineProperties( b, {//这个对象的属性名就是b里边私有属性的属性名
abc: {//abc属性的配置对象
value: 'abc',
writable: true
},
xyz: {
value: 'xyz'
}
} );
console.log(b);//{nickname: "小明", abc: "abc", xyz: "xyz"}
const a = { nickname: 'a' };
Object.defineProperty( a, 'some', {
value: [1,2,3],
writable: true,
enumerable: false,//不可遍历(枚举)
configurable: true //锁是开着的
} );
a.some = 123;
//如果我们想要改some属性的配置
Object.defineProperty( a, 'some', {//因为some属性已经有了,现在是在改some属性的配置
enumerable: true,//不可遍历(枚举)
} );
3.Object.getOwnPropertyDescriptor/getOwnPropertyDescriptors
查看对象里某个私有属性的具体配置 / 查看所有私有属性的配置对象
Object.getOwnPropertyDescriptor
(哪个对象,“哪个私有属性”)Object.getOwnPropertyDescriptor
(哪个对象)–>此时会undefined- 查看时,若该对象没有那个属性,返回undefined
Object.getOwnPropertyDescriptors
(哪个对象)
let obj = {
nickname: 'xiaoming',
age:13
}
Object.defineProperty(obj, 'des', {
value:34
})
console.log(obj);//{nickname: "xiaoming", age: 13, des: 34}
console.log( Object.getOwnPropertyDescriptor(obj,'des') );
//{value: 34, writable: false, enumerable: false, configurable: false}
console.log( Object.getOwnPropertyDescriptor(obj) );//undefined
console.log( Object.getOwnPropertyDescriptors(obj) );
//nickname: {…}, age: {…}, des: {…}}
4.hasOwnProperty()
判断对象身上是否有某个私有属性 返回true或者false
Object.hasOwnProperty('属性名')
const obj4 = { nickname: '小明',age: 3, hobbies: ['sing', 'jump', 'rap', 'basketball'] };
Object.defineProperty( obj4, 'sex', {
value: 'boy'
} );
console.log( 'obj4', obj4);
console.log( obj4.hasOwnProperty('nickname') );//true
console.log( obj4.hasOwnProperty('sex') );//true
console.log( obj4.hasOwnProperty('aaa') );//fasle
console.log( obj4.hasOwnProperty('toString') );//fasle
5.in
判断对象身上是否有某个属性(公有属性和私有属性)
const obj4 = { nickname: '小明',age: 3, hobbies: ['sing', 'jump', 'rap', 'basketball'] };
console.log( obj4.hasOwnProperty('toString') );//fasle
console.log( 'toString' in obj4 );//true
6.Object.freeze()
冻结一个对象 -> 不能添加新的属性,不能修改原来属性的值,也不能更改原来属性的配置,只要被冻住就不能解冻
const obj3 = { nickname: '小刚' };
Object.freeze(obj3);
console.log(obj3);
obj3.nickname = 3;//无效
delete obj3.nickname;//无效
console.log(obj3);
7.Object.isFrozen()
查看对象是否被冻结,返回false或true
console.log( Object.isFrozen(obj3) );
8.Object.seal
密封—>把对象的所有属性的锁都给锁上,不能添加新的属性
const obj1 = { nickname: '小明' };
Object.seal( obj1 );
9.Object.isSealed
查看对象是否被密封
console.log( Object.isSealed(obj1) );
10.Object.entries()
获取所有可枚举属性的键值对数组 Object.entries(对象)
const obj4 = { nickname: '小明',age = 3 };
Object.defineProperty( obj4, 'sex', {
value: 'boy'
} );
let result = Object.entries(obj4)//[Array(2), Array(2)] 获取不到sex
11.Object.fromEntries()
把键值对格式的数组转化为一个对象 Object.fromEntries(键值对格式数组)
const abc = Object.fromEntries(result);
console.log( 'abc', abc);
12.Object.assign()
拷贝别的对象身上的属性 Object.assign(obj1, obj2,obj3...)
把对象2,3…里边的属性合并到1
const obj5 = { nickname: '小明', sex: 'girl' },
obj6 = {
sex: 'boy',
hobbies: ['sing', 'jump', 'rap', 'basketball'],
sayName: function(){
console.log(this.nickname)
}
},
obj7 = { sex: 'girl' };
Object.assign(obj5, obj6, obj7);
console.log(obj5);