对象的扩展----ES6重刷09

对象扩展
一、属性简洁表示

const a = 'a'
const b = () => 'b'
const obj = {a, b}
console.log(obj) // {a: "a", b: ƒ}

二、属性名表达式

let obj = {}
const variable = 'b'
obj.a = 'a'
obj[variable] = 'b'
console.log(obj) // {a: "a", b: "b"}

三、属性特性 — 可配置性(configurable)、可枚举性(enumerable)、可重写性(writable)
首先我们可以通过Object.getOwnPropertyDescriptor(objectName, attrName)得知对应属性的的相关各项信息,如

const obj = {
  a: 'a',
  b: 'b',
  c: 'c',
}
console.log(Object.getOwnPropertyDescriptor(obj, 'a')) 
// {value: "a", writable: true, enumerable: true, configurable: true}

1、可配置性(configurable)作用
当configurable为true时,我们可以使用Object.defineProperty(objectName, attrName, config…)

const obj = {
  a: 'a',
  b: 'b',
  c: 'c',
}
console.log(Object.getOwnPropertyDescriptor(obj, 'a')) 
// {value: "a", writable: true, enumerable: true, configurable: true}
Object.defineProperty(obj, 'a', {
  value: '111',
  writable: true,
  enumerable: true,
  configurable: false,
})
console.log(Object.getOwnPropertyDescriptor(obj, 'a')) 
// {value: "111", writable: true, enumerable: true, configurable: false}
console.log(obj) // {a: "111", b: "b", c: "c"}

当configurable为false时,我们就不可以对相应的属性使用Object.defineProperty(objectName, attrName, config…)

const obj = {
  a: 'a',
  b: 'b',
  c: 'c',
}
console.log(Object.getOwnPropertyDescriptor(obj, 'a')) 
// {value: "a", writable: true, enumerable: true, configurable: true}
Object.defineProperty(obj, 'a', {
  value: '111',
  writable: true,
  enumerable: true,
  configurable: false,
})
console.log(Object.getOwnPropertyDescriptor(obj, 'a')) 
// {value: "111", writable: true, enumerable: true, configurable: false}
Object.defineProperty(obj, 'a', { // Cannot redefine property: a
  value: '111',
  writable: true,
  enumerable: true,
  configurable: true,
})

注意:可配置性(configurable)的修改时单向性的,即当我们修改configurable为false后,就无法更改为true

2、可枚举性(enumerable)作用

const obj = {
  a: 'a',
  b: 'b',
  c: 'c',
}
for ( const item in obj ) {
  console.log(item) // a b c
}
console.log(Object.getOwnPropertyDescriptor(obj, 'a')) 
// {value: "a", writable: true, enumerable: true, configurable: true}
Object.defineProperty(obj, 'a', {
  value: '111',
  writable: true,
  enumerable: false,
  configurable: true,
})
console.log(Object.getOwnPropertyDescriptor(obj, 'a')) 
// {value: "111", writable: true, enumerable: false, configurable: true}
for ( const item in obj ) {
  console.log(item) // b c
}

适用场景:禁止对象中某些元素的可枚举性,实现遍历枚举对象时,避免枚举没必要的属性数据

3、可重写性(writable)作用

const obj = {
  a: 'a',
  b: 'b',
  c: 'c',
}
console.log(Object.getOwnPropertyDescriptor(obj, 'a')) 
// {value: "a", writable: true, enumerable: true, configurable: true}
Object.defineProperty(obj, 'a', {
  value: 'a',
  writable: false,
  enumerable: true,
  configurable: true,
})
console.log(Object.getOwnPropertyDescriptor(obj, 'a')) 
// {value: "111", writable: false, enumerable: true, configurable: true}
console.log(obj) // {a: "a", b: "b", c: "c"}
obj.a = 'abc'
console.log(obj) // {a: "a", b: "b", c: "c"}

适用场景: 冻结对象中的某个属性值

4、Object.freeze(objectName)

const obj = {
  a: 'a',
  b: 'b',
  c: 'c',
}
Object.freeze(obj)
console.log(Object.getOwnPropertyDescriptor(obj, 'a')) 
// {value: "a", writable: false, enumerable: true, configurable: false}

注意:冻结的是整个对象,并且冻结每个属性对应的可配置性(configurable)和可重写性(writable)

四、super关键字【指向的是对象的原型对象】

const proto = {
  foo: 'proto',
}
const obj = {
  a: 'a',
  b: 'b',
  c: 'c',
  find(){
    console.log(super.foo) // proto
    super.foo
  }
}
Object.setPrototypeOf(obj, proto)
obj.find()

五、对象的扩展运算符及解构解析【在此就不多加累述了】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值