Es6-reflect-apply

提供:Dev Guides
移動先:案内検索

ES6-Reflect.apply()

この関数は、argsパラメーターで指定された引数を使用してターゲット関数を呼び出します。

構文

ここで指定された構文は、apply()の構文です。

  • target は呼び出すターゲット関数を表します
  • thisArgument は、ターゲットの呼び出しに提供されるthisの値です。
  • argumentsList は、ターゲットの呼び出しに使用する引数を指定する配列のようなオブジェクトです。
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