Bil*_*oon 7 javascript prototype
// 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 = {}
Run Code Online (Sandbox Code Playgroud)
下一行现在已经破了 - 我该如何解决?
"lorem ipsum".replaceOriginal(/(orem |um)/g,'')
Run Code Online (Sandbox Code Playgroud)
Rob*_*b W 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7764 次 |
| 最近记录: |