// I am trying to make a clone of String's replace function
// and then re-define the replace function (with a mind to
// call the original from the new one with some mods)
String.prototype.replaceOriginal = String.prototype.replace
String.prototype.replace = {}
下一行现已损坏 - 我该如何修复?
"lorem ipsum".replaceOriginal(/(orem |um)/g,'')
最佳答案
唯一可能的问题是你的代码被执行了两次,这会导致问题:真正的原始.replace
会消失。
为避免此类问题,我强烈建议使用以下通用方法替换内置方法:
(function(replace) { // Cache the original method
String.prototype.replace = function() { // Redefine the method
// Extra function logic here
var one_plus_one = 3;
// Now, call the original method
return replace.apply(this, arguments);
};
})(String.prototype.replace);
关于javascript - 如何用自定义函数替换javascript原型(prototype),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10057931/