私は関数を作成し、以下のように引数をバインドしました。Javascript、バインドされた関数の名前を取得
function myFunc(){}
let boundFunc = myFunc.bind(argument);
しかし、その後、私は名前を取得する必要が別の関数への引数として、このバウンド関数を渡します。次
function doTheThing(callable){
console.log(callable.name + " did the thing");
}
doTheThing(boundFunc);
プリントbound did the thing
ではなくmyFunc did the thing
アウト。バインドされた関数の名前を取得する方法はありますか?
callable.caller
Uncaught TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context.
での結果とは、ブラウザの標準ではありません。あなたは短いcallable.name
が行うcallable.name.substring(6)
私が知る限り、 '.bind()'はまったく新しい関数を返します。古い関数のインスタンスをどこかに保持しない限り、その名前を判別することはできません。 –