简体   繁体   English

如何在不丢失原件的情况下更换方法?

[英]How to replace a method without losing the original?

I'm replacing (overriding, improving, adding functionality to) a method in the prototype of the Date object. 我正在Date对象的原型中替换(覆盖,改进,添加功能)方法。 Here is a simplified version of what I've done: 这是我所做的简化版本:

Date.prototype._toString = Date.prototype.toString;

Date.prototype.toString = function(mask) {
    if(mask == undefined){return this._toString();}
    //snip
    //...
    //snip
    return date_string;
}

As I don't want to lose the standard method, I'm assigning the original method to a temporal variable and calling it if appropriate. 因为我不想丢失标准方法,所以我将原始方法分配给时间变量并在适当时调用它。

Is there a way of doing this without polluting the Date.prototype namespace? 有没有办法在不污染Date.prototype命名空间的情况下执行此Date.prototype

What I'm asking is this same question , only in Javascript. 我问的是同样的问题 ,只有在Javascript中。

You can do it like this:- 你可以这样做: -

(function() {
    var _toString = Date.prototype.toString;
    Date.prototype.toString = function(mask) {
       if (mask == undefined) { return _toString.call(this); }
    //snip
    }
 })();

While the above solution is more elegant is the way that it doesn't pollute the Date.prototype, calls to the "new" toString will be slowed down because of the closure it involves. 虽然上面的解决方案更优雅的是它不会污染Date.prototype,但由于它涉及的闭包,对“new”toString的调用将会变慢。

It terms of speed, you'll be better off the way you mentioned in your question. 它的速度方面,你会比你在问题中提到的方式更好。 Try a loop that calls your code 20,000 times and then try the code submitted by Anthony. 尝试一个调用代码20,000次的循环,然后尝试Anthony提交的代码。

Depending on how often your code is called, you'll want to go one way or the other. 根据您的代码调用频率,您可能希望采用这种方式。

Cheers! 干杯!

Update: you can read this little sample from Google 更新:您可以从Google阅读这个小样本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Javascript中添加/替换嵌套对象中的值(不丢失原始引用)? - How to add / replace values in a nested object (without losing original references) in Javascript? 如何在JavaScript中扩展Object而不会丢失原始功能 - How to extend Object in JavaScript without losing the original functionality 如何在不丢失原始格式的情况下将日期更改为字符串? - How to change a date to a string without losing the original formatting? 您如何在不失去焦点的情况下替换内容? - How do you replace content without losing focus? 替换数组元素而不会丢失引用? - Replace array elements without losing reference? 在不丢失事件的情况下查找+替换元素的内容 - Find + replace content of an element without losing events 如何在内容可编辑div中替换字符串而不丢失已经对部分文本执行的文本格式 - How to replace string without losing text format already done to a part of text in content editable div 如何替换包含HTML标签的单词而不丢失HTML标签? - How can I replace words that contain HTML tags without losing the HTML tags? 如何在 class 的方法内的事件侦听器中使用“this”关键字,而不会失去对 class arguments 和方法 ZDBC11CAA5BDA99F77E6FBDABDBDBDA99F77E6FBDABDBD77 的访问权限 - How to use “this” keyword inside event listener inside a method of a class without losing access to class arguments and method arguments? 下划线删除值而不替换原始变量 - Underscore Remove the Value without replace original variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM