How to override global functions in JavaScript, but keep a reference to the original function?
<p>I'd like to implement a custom function for window.confirm, so I don't have to rewrite a large amount of legacy code, but be able to fallback to the original function if something goes wrong, or depending on some arbitrary logic.</p>

<p>A quick stub code, just for example (probably won't do it like this, but just for an idea):</p>

<pre><code>window.confirm = function(message, successCallback){
  var ok = site.UI.confirmDialog(message);
  if (ok &amp;&amp; typeof(successCallback) == 'function'){
      successCallback();
  } else {
     // maybe call original browser confirm?
     // window.confirmOriginal...?
  }
}
</code></pre>

<p>The question here is, how to get the original function if I'm overwriting it? Does it exist somewhere on the prototype, or am I looking at this all the wrong way?</p>

<p>Thanks.</p>
