Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
了解Javascript中的猴子补丁_Javascript_Monkeypatching - Fatal编程技术网

了解Javascript中的猴子补丁

了解Javascript中的猴子补丁,javascript,monkeypatching,Javascript,Monkeypatching,我试图理解monkey补丁在JavaScript中的工作原理 我看了太多的例子,但无法理解 例如,猴子在Redux中修补调度功能 资料来源: 有人能用简单的术语和例子解释一下猴子补丁吗 使用它的最佳方案是什么 谢谢。假设您使用一个库来定义类测试和方法测试 如果要对其进行修补,则必须使用此类代码,并将其包含在库之后: // replacing current implementation with a new one Test.prototype.test = function(arg1, arg

我试图理解monkey补丁在JavaScript中的工作原理

我看了太多的例子,但无法理解

例如,猴子在Redux中修补调度功能

资料来源:

有人能用简单的术语和例子解释一下猴子补丁吗

使用它的最佳方案是什么


谢谢。

假设您使用一个库来定义类测试和方法测试

如果要对其进行修补,则必须使用此类代码,并将其包含在库之后:

// replacing current implementation with a new one
Test.prototype.test = function(arg1, arg2, ...){...}
现在,假设您想做一些更聪明的事情,比如在不修改其余部分的情况下向函数中添加一些内容,下面是您将如何做的:

var oldFN = Test.prototype.test;
Test.prototype.test = function([arguments...]){
    [...some custom code...]
    oldFN.apply(this, arguments);// arguments keyword refer to the list of argument that your function recevied, if you need something else use call.
    [...some custom code...]
}

猴子补丁是有效的,但必须明智地使用。此外,每次升级库时,都必须检查所有修补程序是否正常工作。

要以更简单的方式理解,请访问本网站:
var oldFN = Test.prototype.test;
Test.prototype.test = function([arguments...]){
    [...some custom code...]
    oldFN.apply(this, arguments);// arguments keyword refer to the list of argument that your function recevied, if you need something else use call.
    [...some custom code...]
}