Error prototype hacking

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/json2/20121008/json2.min.js"></script>
<h3>Hacking the `Error.prototype` to polyfill `window.onerror`</h3>

<div id="results"></div>

CSS

#results {
    font-size: 8pt;
}

JavaScript

/*
Hacking the `Error.prototype` to polyfill `window.onerror`

GOTCHAS
=========
   WARNING: Thar be dragons!

   If you rely on this technique in a browser that doesn't invoke `toString` on the Error
   instance when it propagates to `window.onerror`, you will be getting false information
   as the `window.__lastError__` would only reflect the last time some other code/library
   exercised some `error.toString()`. It would be best to develop some manner of feature
   test to check this in advance, or else at least by detecting the browser (lame but better
   than returning false positives!). One more option would be comparing the value of
   `message` against the the contents of the Error instance's `message` property,
   e.g.
      var isCorrectError = (function() {
          var messageMatch = message.indexOf(window.__lastError__.message) !== -1;
          var crossOriginError = message === "Script error.";
          var ieIsStupidError = message === "Exception thrown and not caught";
          return messageMatch && !crossOriginError && !ieIsStupidError;
      })();
   
   This also will not work on Error classes that have their own `toString` method that is
   not inherited from `Error.prototype` (`e.toString !== Error.prototype.toString`).


STATUS
========
 - Chrome 30:
     - Win7:
         - Doesn't need the fix as it supports the latest spec for `window.onerror` already.
         - `Error.prototype.toString` not invoked. =(
         - Polyfillable: NO but unnecessary because it follows the spec!
 - Firefox 24:
     - Win7:
         - `window.onerror` still missing `columnNumber` and `error`.
         - `Error.prototype.toString` invoked and contains all data! =)
         - Polyfillable: FULL
 - Firefox 23:
     - OS X Mountain Lion:
         - `window.onerror` still missing `columnNumber` and `error`.
         - `Error.prototype.toString` invoked and contains all data! =)
         - Polyfillable: FULL
 - Opera 12.16:
     - Win7:
         -...