Object.getOwnPropertyDescriptor() method returns an object describing the configuration of a specific property on a given object (that is, one directly present on an object and not in the object's prototype chain). The object returned is mutable but mutating it has no effect on the original property's configuration.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

Object.getOwnPropertyDescriptor(obj, prop)
					

参数

obj

The object in which to look for the property.

prop
The name or Symbol of the property whose description is to be retrieved.

返回值

A property descriptor of the given property if it exists on the object, undefined 否则。

描述

This method permits examination of the precise description of a property. A property in JavaScript consists of either a string-valued name or a Symbol and a property descriptor. Further information about property descriptor types and their attributes can be found in Object.defineProperty() .

A property descriptor is a record with some of the following attributes:

value

The value associated with the property (data descriptors only).

writable
true if and only if the value associated with the property may be changed (data descriptors only).
get
A function which serves as a getter for the property, or undefined if there is no getter (accessor descriptors only).
set
A function which serves as a setter for the property, or undefined if there is no setter (accessor descriptors only).
configurable
true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.
enumerable
true if and only if this property shows up during enumeration of the properties on the corresponding object.

范例

Using Object.getOwnPropertyDescriptor

var o, d;
o = { get foo() { return 17; } };
d = Object.getOwnPropertyDescriptor(o, 'foo');
// d is {
//   configurable: true,
//   enumerable: true,
//   get: /*the getter function*/,
//   set: undefined
// }
o = { bar: 42 };
d = Object.getOwnPropertyDescriptor(o, 'bar');
// d is {
//   configurable: true,
//   enumerable: true,
//   value: 42,
//   writable: true
// }
o = { [Symbol.for('baz')]: 73 }
d = Object.getOwnPropertyDescriptor(o, Symbol.for('baz'));
// d is {
//   configurable: true,
//   enumerable: true,
//   value: 73,
//   writable: true
// }
o = {};
Object.defineProperty(o, 'qux', {
  value: 8675309,
  writable: false,
  enumerable: false
});
d = Object.getOwnPropertyDescriptor(o, 'qux');
// d is {
//   value: 8675309,
//   writable: false,
//   enumerable: false,
//   configurable: false
// }
						

Non-object coercion

In ES5, if the first argument to this method is not an object (a primitive), then it will cause a TypeError . In ES2015, a non-object first argument will be coerced to an object at first.

Object.getOwnPropertyDescriptor('foo', 0);
// TypeError: "foo" is not an object  // ES5 code
Object.getOwnPropertyDescriptor('foo', 0);
// Object returned by ES2015 code: {
//   configurable: false,
//   enumerable: true,
//   value: "f",
//   writable: false
// }
						

规范

规范
ECMAScript (ECMA-262)
The definition of 'Object.getOwnPropertyDescriptor' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
getOwnPropertyDescriptor Chrome 5 Edge 12 Firefox 4 IE 9
9
部分支持 8
In Internet Explorer 8, this was only supported on DOM objects and with some non-standard behaviors. This was later fixed in Internet Explorer 9.
Opera 12 Safari 5 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 12 Safari iOS 5 Samsung Internet Android 1.0 nodejs Yes

图例

完整支持

完整支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: