JavaScript 对象(Objects) 方法

Object.getOwnPropertyDescriptors()方法返回给定对象的所有自己属性的描述符。 

getOwnPropertyDescriptors() 和 getOwnPropertyDescriptor() 方法的区别在于 getOwnPropertyDescriptors() 方法忽略了符号属性。

语法:

Object.getOwnPropertyDescriptors(obj)

参数

obj:对象。

返回:

此方法返回一个对象,该对象包含对象的所有自己的属性描述符。如果没有属性,此方法可能会返回一个空对象。

浏览器支持

chrome54
edge15
firefox50
opera41

示例

下面介绍一些例子更好的理解该方法的使用。

示例1

const object1 = {
  property1: 103
};

const descriptors1 = Object.getOwnPropertyDescriptors(object1);
console.log(descriptors1.property1.writable);
console.log(descriptors1.property1.value);

输出:

103

示例2

const object1 = {
  property1: 22
};
const descriptors1 = Object.getOwnPropertyDescriptors(object1);
console.log(descriptors1.property1.value);
console.log(descriptors1.property1);
console.log(descriptors1.property1.writable);

输出:

[object Object] {
configurable: true,
enumerable: true,
value: 22,
writable: true
}
true

示例3

const object1 = {
  property1: 42
};
const object2 = {
  property2: 23
};

const descriptors1 = Object.getOwnPropertyDescriptors(object1);
const descriptors2 = Object.getOwnPropertyDescriptors(object2);
console.log(descriptors1.property1.writable);
console.log(descriptors1.property1.value,descriptors2.property2.value);

输出:

true
42
23