Object defineProperties()方法

Creates or configures multiple object properties at once. Returns the object.

一次创建或配置多个对象属性。 返回对象。

Takes 2 arguments. The first is an object upon which we’re going to create or configure the properties. The second is an object of properties.

接受2个参数。 第一个是我们将在其上创建或配置属性的对象。 第二个是属性对象。

Example:

例:

const dog = {}
Object.defineProperties(dog, {
  breed: {
    value: 'Siberian Husky'
  }
})
console.log(dog.breed) //'Siberian Husky'

I didn’t just say breed: 'Siberian Husky' but I had to pass a property descriptor object, defined at the beginning of this page.

我不只是说breed: 'Siberian Husky'而是我必须传递在页面开头定义的属性描述符对象。

It can be used in conjunction with Object.getOwnPropertyDescriptors() to copy properties over from another object:

它可以与Object.getOwnPropertyDescriptors()结合使用,以从另一个对象复制属性:

const wolf = { /*... */ }
const dog = {}
Object.defineProperties(dog, Object.getOwnPropertyDescriptors(wolf))

翻译自: https://flaviocopes.com/javascript-object-defineproperties/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值