Skip to content

Instantly share code, notes, and snippets.

@shizuka-na-kazushi
Created September 19, 2020 23:56
Show Gist options
  • Save shizuka-na-kazushi/d2fedf9e24d45808eca9c6807295ee21 to your computer and use it in GitHub Desktop.
Save shizuka-na-kazushi/d2fedf9e24d45808eca9c6807295ee21 to your computer and use it in GitHub Desktop.
関数呼び出しをapplyハンドラとProxyで置き換える
// 元の関数を定義
function showtime(season) {
console.log(`トムとジェリーのシーズン ${season}をみる`);
}
// 元の関数を呼び出し
showtime(1);
showtime(2);
showtime(3);
// ハンドラー (apply)を定義し、元の関数を新しいパラメータで呼び出す
var handler = {
apply: (target, thisArg, argumentsList) => {
return target(59);
}
};
// 元の関数とhandlerを引数にProxyを生成
showtime = new Proxy(showtime, handler);
// proxyで置き換えた関数を呼び出す
showtime(1);
showtime(2);
showtime(3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment