Reflect.get()方法与从 对象 (target[propertyKey]) 中读取属性类似,但它是通过一个函数执行来操作的。
语法
Reflect.get(target, propertyKey[, receiver])
参数
-
target - 需要取值的目标对象
-
propertyKey - 需要获取的值的键值
- receiver
-
如果
target对象中指定了getter,receiver则为getter调用时的this值。
返回值
属性的值。
异常
如果目标值类型不是 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
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
get |
Chrome Full support 49 | Edge Full support 12 | Firefox Full support 42 | IE No support No | Opera Full support 36 | Safari Full support 10 | WebView Android Full support 49 | Chrome Android Full support 49 | Firefox Android Full support 42 | Opera Android Full support 36 | Safari iOS Full support 10 | Samsung Internet Android Full support 5.0 | nodejs Full support 6.0.0 |
Legend
- Full support
- Full support
- No support
- No support