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/