Javascript 覆盖函数(例如“警报”)并调用原始函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10427708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Override function (e.g. "alert") and call the original function?
提问by Rob W
I would like to override a Javascript built-in function with a new version that calls the original (similarly to overriding a method on a class with a version that calls super
in many languages). How can I do this?
我想用一个调用原始函数的新版本来覆盖一个 Javascript 内置函数(类似于用一个super
以多种语言调用的版本覆盖一个类上的方法)。我怎样才能做到这一点?
For example...
例如...
window.alert = function(str) {
//do something additional
if(console) console.log(str);
//super.alert(str) // How do I do this bit?
}
回答by Rob W
Store a reference to the original function in a variable:
将原始函数的引用存储在变量中:
(function() {
var _alert = window.alert; // <-- Reference
window.alert = function(str) {
// do something additional
if(console) console.log(str);
//return _alert.apply(this, arguments); // <-- The universal method
_alert(str); // Suits for this case
};
})();
The universal way is <original_func_reference>.apply(this, arguments)
- To preserve context and pass all arguments. Usually, the return value of the original method should also be returned.
通用方法是<original_func_reference>.apply(this, arguments)
- 保留上下文并传递所有参数。通常,原始方法的返回值也应该返回。
However, it's known that alert
is a void function, takes only one argument, and does not use the this
object. So, _alert(str)
is sufficient in this case.
然而,众所周知它alert
是一个空函数,只接受一个参数,并且不使用this
对象。所以,_alert(str)
在这种情况下就足够了。
Note: IE <= 8 throws an error if you try to overwrite alert
, so make sure that you're using window.alert = ...
instead of alert = ...
.
注意:如果您尝试覆盖 IE <= 8 会引发错误alert
,因此请确保您使用的是window.alert = ...
而不是alert = ...
.
回答by Rob W
There is no "super". Anyway, create a closure to "keep" around the original function-object.
没有“超级”。无论如何,创建一个闭包来“保留”原始函数对象。
Note the "self invoking function" that returns a new function-object (that is assigned to the window.alert
property). The new function-object returned creates a closure around the variableoriginal
which evaluates to the original valueof window.alert
that was passed in to the "self invoking function".
请注意“自调用函数”,它返回一个新的函数对象(分配给window.alert
属性)。返回的新函数对象周围产生一个闭合变量original
,其计算结果为原始值的window.alert
,在“自主调用功能”获得通过。
window.alert = (function (original) {
return function (str) {
//do something additional
if(console) {
console.log(str)
}
original(str)
}
})(window.alert)
However, I believesome browsers may prevent alert
and other built-ins from being modified...
但是,我相信某些浏览器可能会阻止alert
和其他内置程序被修改...
Happy coding.
快乐编码。
回答by Art
I'm assuming your question is how do you overwrite a built-in and still be able to call it. First off as a disclaimer, you should never overwrite built ins unless you have a good reason for doing it since it will make it impossible to debug/test.
我假设您的问题是如何覆盖内置函数并仍然能够调用它。首先作为免责声明,除非您有充分的理由这样做,否则您永远不应该覆盖内置插件,因为它会使调试/测试变得不可能。
This is how you would do it:
这是你会怎么做:
window._alert = window.alert;
window.alert = function(str) {
if(console) console.log(str);
window._alert(str);
}
回答by Poul Krogh
How to do simple classical inheritance in Javascript:
如何在 Javascript 中进行简单的经典继承:
SuperClass.call(this) // inherit from SuperClass (multiple inheritance yes)
How to override functions:
如何覆盖函数:
this.myFunction = this.myFunction.override(
function(){
this.superFunction(); // call the overridden function
}
);
The override function is created like this:
覆盖函数是这样创建的:
Function.prototype.override = function(func)
{
var superFunction = this;
return function()
{
this.superFunction = superFunction;
return func.apply(this,arguments);
};
};
Works with multiple arguments.
Fails when trying to override undefined or nonfunctions.
Makes "superFunction" a "reserved" word :-)
适用于多个参数。
尝试覆盖未定义或非函数时失败。
使“superFunction”成为“保留”字:-)
回答by David M
JavaScript does not use a classical inheritance model. There is a nice article herewhich describes a way to write your classes so that a similar syntax can be used, but it's not natively supported.
JavaScript 不使用经典的继承模型。这里有一篇很好的文章,它描述了一种编写类的方法,以便可以使用类似的语法,但它本身并不支持。