handler.apply()
handler.apply()
メソッドは関数呼び出しに対するトラップです。
試してみましょう
構文
const p = new Proxy(target, { apply: function(target, thisArg, argumentsList) { } });
引数
次の引数が apply()
メソッドに渡されます。 this
はハンドラーにバインドされます。
target
-
ターゲットオブジェクト
thisArg
-
この呼び出しに対する
this
引数 argumentsList
-
この呼び出しに対する引数リスト
返値
apply()
メソッドはどんな値でも返すことができます。
解説
handler.apply()
メソッドは関数呼び出しに対するトラップです。
介入
このトラップは下記の操作に介入できます。
proxy(...args)
Function.prototype.apply()
andFunction.prototype.call()
Reflect.apply()
不変条件
以下の不変条件に違反している場合、プロキシは TypeError
を発生します。
target
は呼び出し可能、つまり関数でなければなりません。
例
関数呼び出しのトラップ
次のコードでは、関数呼び出しをトラップします。
const p = new Proxy(function() {}, {
apply: function(target, thisArg, argumentsList) {
console.log('called: ' + argumentsList.join(', '));
return argumentsList[0] + argumentsList[1] + argumentsList[2];
}
});
console.log(p(1, 2, 3)); // "called: 1, 2, 3"
// 6
仕様書
Specification |
---|
ECMAScript Language Specification # sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist |
ブラウザーの互換性
BCD tables only load in the browser