title: Replace window or document objects with Javascript object
body: <p>Is there a way to replace "window" or "document" objects? What I basically want is to provide some kind of JavaScript Proxy, I want to prevent user from getting "SOME" (only some! this is important) DOM element's on the page. By "user" I mean any third patty script. </p>
  
  <p>I can do this:</p>
  
  <pre><code>document.getElementsByTagName("a")
  //NodeList[129]
  document.getElementsByTagName = function(){}
  //function (){}
  document.getElementsByTagName("a")
  //undefined
  </code></pre>
  
  <p>But what I can do about <code>document.all</code> how can I replace DOM object field to make it return only "SOME" of the DOM elements?</p>
  
  <p><strong>UPD</strong>: If there is a way to replace 'document' object with some JavaScript object this would be much more better</p>
  
  <p><strong>UPD2</strong>: I don't care if your method won't work on 'older' browsers. So i'm good with any solution that works on "A" graders</p>
  
  <p><strong>UPD3</strong>: I know that 100% security doesn't exist in JavaScript, I don't want to prevent hackers from "HACKING", I know it's not possible, I want to prevent developers that write "plugin" for my "home made" framework, to do stupid things.. </p>
  
  <p><strong>UPD4</strong>: ok, I can't replace Document nor Window, but can I at least replace all "fields" or "functions" that repsonsoble for returning "DOM" elements? like "document.getElementById" or "document.all" ?</p>
  
  <p><strong>UPD5</strong>: User <code>@pebbl</code> suggested something that 'close' to the thing I want</p>
  
  <pre><code>function a(window, document){
     /// in here window and document should be numerics
     alert(window);
     alert(document);
  }
  
  a(123,456);
  </code></pre>
  
  <p>but his solution have one big problem <a href="http://jsfiddle.net/kRLax/" rel="nofollow">http://jsfiddle.net/kRLax/</a></p>
  
  <p><strong>UPD6-7</strong>: This is the 'perfect' one (at least for me)</p>
  
  <p><a href="http://jsfiddle.net/kRLax/12/" rel="nofollow">http://jsfiddle.net/kRLax/12/</a></p>
  
  <pre><code>function Fx(){return function(){}}
  function SafeThis(that){ 
      if (that == window) {
          return fakeWindow;
      } else if (that = document) {
          return fakeDocument;                    
      } else {
          return that;
      }
  }
  var fakeDocument = {
      write: function(a){ document.write(a) }
  }
  var fakeWindow = {
      document: fakeDocument
  }
  
  var moduleA = function(Function, window, document, eval){
      document.write(window + "&lt;br&gt;");
      var f = new Function("return this");
      document.write(f() + "&lt;br&gt;");
      var win = (function(){return this;})();
      document.write(win + "&lt;br&gt;");
      var e = eval("this"); 
      document.write(e + "&lt;br&gt;");
      document.write(this + "&lt;br&gt;");
      document.write(window + "&lt;br&gt;");
      document.write(document + "&lt;br&gt;");
      this.a = 1;
      document.write(JSON.stringify(this));
  };
  
  var moduleA_Fx = '!' + 
      moduleA.toString().replace(/\bthis\b/g,"SafeThis(this)") + 
      '(Fx,fakeWindow,fakeDocument,Fx)';
  
  document.write(moduleA_Fx + "&lt;br&gt;&lt;br&gt;");
  
  eval(moduleA_Fx);
  ​
  </code></pre>