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
廣告
© . All rights reserved.