Reflect.get()
JavaScript Reflect.get()方法
静态的
Reflect.get()方法用于从对象中检索函数的属性。第一个参数是对象,第二个参数是属性名称。
语法:
Reflect.get(target, propertyKey[, receiver])
参数:
target:它是获取属性的目标对象。
propertyKey:这是获取密钥的名称。
receiver:这是在遇到吸气剂时为对象调用提供的值。
返回值:
它返回属性的值。
例外:
如果目标不是对象,则为TypeError。
浏览器支持:
浏览器 |
版本 |
Chrome |
49 |
Edge |
12 |
Firefox |
42 |
Opera |
36 |
示例1
const u = {p:3};
console.log( Reflect.get ( u , "p" ) === 3 );
// if property key is not found, return undefined just like obj.key
console.log( Reflect.get ( u , "h" ) === undefined );
console.log( Reflect.get ( u , "h" ) === 3 );
输出:
示例2
const x = {p:3};
const y = Object.create (x);
// x is parent of y
console.log (
Reflect.get ( y, "p" ) === 3
// Reflect.get will traverse the prototype chain to find property
);
输出:
示例3
const object1 = {
x: 1,
y: 2
};
console.log(Reflect.get(object1, 'y'));
// expected output: 1
var array1 = ['zero', 'one','Carry','marry','charry'];
console.log(Reflect.get(array1, 4));
输出: