From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../reflect/getownpropertydescriptor/index.html | 139 +++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/reflect/getownpropertydescriptor/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/reflect/getownpropertydescriptor') diff --git a/files/zh-cn/web/javascript/reference/global_objects/reflect/getownpropertydescriptor/index.html b/files/zh-cn/web/javascript/reference/global_objects/reflect/getownpropertydescriptor/index.html new file mode 100644 index 0000000000..e999254e32 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/reflect/getownpropertydescriptor/index.html @@ -0,0 +1,139 @@ +--- +title: Reflect.getOwnPropertyDescriptor() +slug: Web/JavaScript/Reference/Global_Objects/Reflect/getOwnPropertyDescriptor +translation_of: Web/JavaScript/Reference/Global_Objects/Reflect/getOwnPropertyDescriptor +--- +
{{JSRef}}
+ +

静态方法 Reflect.getOwnPropertyDescriptor() 与 {{jsxref("Object.getOwnPropertyDescriptor()")}} 方法相似。如果在对象中存在,则返回给定的属性的属性描述符。否则返回 {{jsxref("undefined")}}。 

+ +

语法

+ +
Reflect.getOwnPropertyDescriptor(target, propertyKey)
+
+ +

参数

+ +
+
target
+
需要寻找属性的目标对象。
+
propertyKey
+
获取自己的属性描述符的属性的名称。
+
+ +

返回值

+ +

如果属性存在于给定的目标对象中,则返回属性描述符;否则,返回 {{jsxref("undefined")}}。

+ +

异常

+ +

抛出一个 {{jsxref("TypeError")}},如果目标不是 {{jsxref("Object")}}。

+ +

描述

+ +

Reflect.getOwnPropertyDescriptor方法返回一个属性描述符,如果给定的属性存在于对象中,否则返回 {{jsxref("undefined")}} 。 与  {{jsxref("Object.getOwnPropertyDescriptor()")}} 的唯一不同在于如何处理非对象目标。

+ +

示例

+ +

使用 Reflect.getOwnPropertyDescriptor()

+ +
Reflect.getOwnPropertyDescriptor({x: "hello"}, "x");
+// {value: "hello", writable: true, enumerable: true, configurable: true}
+
+Reflect.getOwnPropertyDescriptor({x: "hello"}, "y");
+// undefined
+
+Reflect.getOwnPropertyDescriptor([], "length");
+// {value: 0, writable: true, enumerable: false, configurable: false}
+
+ +

与 Object.getOwnPropertyDescriptor() 的不同点

+ +

如果该方法的第一个参数不是一个对象(一个原始值),那么将造成 {{jsxref("TypeError")}} 错误。而对于 {{jsxref("Object.getOwnPropertyDescriptor")}},非对象的第一个参数将被强制转换为一个对象处理。

+ +
Reflect.getOwnPropertyDescriptor("foo", 0);
+// TypeError: "foo" is not non-null object
+
+Object.getOwnPropertyDescriptor("foo", 0);
+// { value: "f", writable: false, enumerable: true, configurable: false }
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES6', '#sec-reflect.getownpropertydescriptor', 'Reflect.getOwnPropertyDescriptor')}}{{Spec2('ES6')}}Initial definition.
{{SpecName('ESDraft', '#sec-reflect.getownpropertydescriptor', 'Reflect.getOwnPropertyDescriptor')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support49{{CompatGeckoDesktop(42)}}{{CompatNo}}{{CompatNo}}10
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatNo}}49{{CompatGeckoMobile(42)}}{{CompatNo}}{{CompatNo}}10
+
+ +

相关链接

+ + -- cgit v1.2.3-54-g00ecf