非常教程

JavaScript参考手册

Reflect

Reflect.getOwnPropertyDescriptor

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

语法

Reflect.getOwnPropertyDescriptor(target, propertyKey)

参数

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

返回值

如果属性存在于给定的目标对象中,则返回属性描述符;否则,返回 undefined

异常

抛出一个TypeError,如果目标不是 Object

描述

Reflect.getOwnPropertyDescriptor 方法返回一个属性描述符,如果给定的属性存在于对象中,否则返回 undefined。 与  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() 的不同点

如果该方法的第一个参数不是一个对象(一个原始值),那么将造成 TypeError错误。而对于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 }

规范

Specification

Status

Comment

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Reflect.getOwnPropertyDescriptor' in that specification.

Standard

Initial definition.

ECMAScript Latest Draft (ECMA-262)The definition of 'Reflect.getOwnPropertyDescriptor' in that specification.

Draft

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

49

(Yes)

42 (42)

No support

No support

10

Feature

Android

Chrome for Android

Edge

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

No support

49

(Yes)

42.0 (42)

No support

No support

10

JavaScript

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络