title: Overriding an object prototype and disallowing any further overrides
body: <p>I'm trying to write a library that intercepts all XMLHttpRequest calls and do something before eventually sending out the request, by overriding its prototype, for example:</p>
  
  <pre><code>var original_open = XMLHttpRequest.prototype.open;    
  XMLHttpRequest.prototype.open = function() {
      // my own override logic here before running the original function
      original_open.apply(this, arguments);
  };
  </code></pre>
  
  <p>The problem is, I want to <strong>guarantee that when someone uses this library, it is impossible for any other code on the web page to re-override this effect.</strong></p>
  
  <p>Because otherwise, the website using this library can dynamically load another piece of JS code which simply overrides the <code>XMLHttpRequest.prototype.open</code> again, and the whole purpose of this library is to disallow that.</p>
  
  <p>I thought about freezing the prototype using <code>Object.freeze()</code> right after the override, so that no other code can override my own override. The code would look something like this:</p>
  
  <pre><code>var original_open = XMLHttpRequest.prototype.open;    
  XMLHttpRequest.prototype.open = function() {
      // my own override logic here before running the original function
      original_open.apply(this, arguments);
  };
  Object.freeze(XMLHttpRequest.prototype);
  </code></pre>
  
  <p>My expectation is that, if another piece of code on the same page tries to re-override my own override, it will fail because the prototype has been frozen.</p>
  
  <h2>My question:</h2>
  
  <ol>
  <li>Is this the correct solution for this problem?</li>
  <li>Is this truly secure? (No loopholes)?</li>
  </ol>