ES6 - Reflect.apply() 函数

此函数使用 args 参数指定的参数调用目标函数。

语法

此处给出的语法适用于 apply(),其中,

  • target 表示要调用的目标函数

  • thisArgument 是为调用 target 提供的 this 值。

  • argumentsList 是一个类似数组的对象,指定应使用哪个参数调用 target。

Reflect.apply(target, thisArgument,argumentsList)

示例

以下示例定义了一个计算并返回矩形面积的函数。

<script>
   const areaOfRectangle = function(width,height){
      return `area is ${width*height} ${this.units}`
   }
   const thisValue = {
      units:'Centimeters'
   }
   const argsList = [10,20]
   const result = Reflect.apply(areaOfRectangle,thisValue,argsList)

   console.log(result)
</script>

上述代码的输出将如下所示 −

area is 200 Centimeters