猴子修补XMLHttpRequest.prototype.open和"触摸"参数

Ily*_*kov 12 javascript internet-explorer monkeypatching xmlhttprequest

我正在尝试为XMLHttpRequest.prototype.open在IE8兼容模式下运行的Intranet站点添加补丁程序,但它一直在投掷SCRIPT438: Object doesn't support this property or method.奇怪的是......如果我"触摸"第arguments一个,即取消注释bar,它就可以正常工作!有谁知道为什么,如果触摸它确实解决了100%的情况下的问题?

var foo = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function() {
  //var bar = arguments;
  foo.apply(this, arguments);
  console.log("OK");
}
Run Code Online (Sandbox Code Playgroud)

这是在IE8模式中的IE9现代.用VM图像搜索的VM截图试图open在滚动时修补猴子的请求.

在此输入图像描述

编辑:

console.log(foo);
//console.log(foo.apply);
console.log(typeof foo);
console.log(foo instanceof Function);
Run Code Online (Sandbox Code Playgroud)

返回

LOG: 
function open() {
    [native code]
}

LOG: object 
LOG: false  
Run Code Online (Sandbox Code Playgroud)

console.log(foo.apply)一个抛出"Object doesn't support this property or method".

有趣的是,我无法在我试过的任何模式下在实际的IE8虚拟机中复制这个,只能在运行IE8标准模式的IE9中.

Hei*_*erg 0

我最近刚刚看到一个XMLHttpRequest.prototype.open被覆盖的例子,其方法与你的方法略有不同;

(function(open) {
    XMLHttpRequest.prototype.open = function() {
        // your special sauce

        open.apply(this, arguments);
    };
})(XMLHttpRequest.prototype.open);
Run Code Online (Sandbox Code Playgroud)

你能检查一下这样做是否会有不同的效果吗?