<!DOCTYPE html>
<html>

<head>
<script>
// 引数の functionBefore を実行してから、本体 function を実行する
// function を返す関数
// beforeの返値は変更した引数で、この引数が本体 function に渡される
Function.prototype.before = function( functionBefore ) {
  var _this = this;
  return function() {
    var processedArgs = functionBefore.apply(this, arguments);
    return _this.apply(this, processedArgs);
  }
};

// 本体 function を実行してから、引数の functionAfter を実行する、
// function を返す関数
// after の引数は処理結果
Function.prototype.after = function( functionAfter ) {
  var _this = this;
  return function() {
    var result = _this.apply(this, arguments);
    functionAfter.apply(this, result);
    return result;
  }
};

// window.open を、beforeを実行してから、window.open を実行する
// function に差し替える
window.open = window.open.before(function(){
  console.log('start!');
  console.log(arguments);
  arguments[0] = arguments[0] + "?q=AOP";
  return arguments;
});

// window.open を、window.open を実行してから afterを実行する
// function に差し替える
window.open = window.open.after(function(result){
  console.log('end!');
  console.log(result);
});

function openIt() {
  window.open("https://www.google.com/search");
}

</script>
</head>

<body>

<button onclick="openIt()">OPEN</button>

</body>

</html>

HTML


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2020-09-28 (月) 23:26:37 (1298d)
Short-URL:
ISBN10
ISBN13
9784061426061