在此代码中,我创建了一个名为someFunction的函数。然后我修改了Function.prototype.apply并调用方法。所以,而不是我的功能代码正在工作我正在运行我的拦截代码(显示警报)。但是“呼叫”和“应用”都不会拦截直接方法调用。拦截这个是否正确?我可以拦截直接调用的函数吗?
Function.prototype.call = function(){alert("call");};
Function.prototype.apply = function(){alert("apply");};
function someFunction(){}
window.onload = function(){
someFunction.call(this); //call alert is shown
someFunction.apply(this); //apply alert is shown
someFunction(); //how can I intercept this?
}
也许我可以改变Function.constructor功能,所以每一个返回的函数将围绕它的包装。 – yilmazhuseyin 2010-08-04 14:36:32
@yilmazhuseyin:不,你不能。更改* Function *构造函数只会允许您覆盖使用'new Function(str)'创建的函数。 – 2010-08-04 14:49:03
是的你是对的。函数newConstructor(){alert(“a”);}; Function.prototype.constructor = newConstructor; 没有工作。 – yilmazhuseyin 2010-08-04 14:56:41