// 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,'')
答案 0 :(得分:20)
唯一可能的问题是您的代码执行了两次,这会导致问题:真正的原始.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);