Reflect.get()

Reflect.get()方法与从 对象 (target[propertyKey]) 中读取属性类似,但它是通过一个函数执行来操作的。

语法

Reflect.get(target, propertyKey[, receiver])

参数

target
需要取值的目标对象
propertyKey
需要获取的值的键值
receiver
如果target对象中指定了getterreceiver则为getter调用时的this值。

返回值

属性的值。

异常

如果目标值类型不是 Object,则抛出一个 TypeError

描述

Reflect.get方法允许你从一个对象中取属性值。就如同属性访问器 语法,但却是通过函数调用来实现。

实例

使用 Reflect.get()

// Object
var obj = { x: 1, y: 2 };
Reflect.get(obj, "x"); // 1

// Array
Reflect.get(["zero", "one"], 1); // "one"

// Proxy with a get handler
var x = {p: 1};
var obj = new Proxy(x, {
  get(t, k, r) { return k + "bar"; }
});
Reflect.get(obj, "foo"); // "foobar"

规范

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
Reflect.get
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
Reflect.get
Draft

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
getChrome Full support 49Edge Full support 12Firefox Full support 42IE No support NoOpera Full support 36Safari Full support 10WebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 42Opera Android Full support 36Safari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 6.0.0

Legend

Full support  
Full support
No support  
No support

相关链接