Reflect.apply()
JavaScript Reflect.apply()方法
JavaScript的静态
Reflect.apply()方法用于使用指定的参数调用函数。
语法
Reflect.apply(target, thisArgument, argumentsList)
参数
target:它是要调用的目标函数。
thisArgument:
ArgumentsList :它是一个类似数组的对象,用于指定应调用目标的参数。
返回
使用指定的this值和参数调用给定目标函数的结果。
异常
如果无法调用目标,则此方法将引发
TypeError 。
示例1
function g (a, b) {
this.x = a;
this.y = b;
}const obj = {};
Reflect.apply ( g , obj, [33,44] );
console.log( obj );
输出:
示例2
var whatsThis = function() { console.log(this); }
Reflect.apply(whatsThis, 'hello', []);
// Call a function that takes a variable number of args
var numbers = [3, 20, 1, 55];
console.log(Reflect.apply(Math.max, undefined, numbers));
输出:
示例3
console.log(Reflect.apply(Math.floor, undefined, [45]));
console.log(Reflect.apply(String.fromCharCode, undefined, [104, 101,103,105]));
console.log(Reflect.apply(RegExp.prototype.exec, /ab/, ['confabulation']).index);
console.log(Reflect.apply(''.charAt, 'Rahul', [3]));
输出: