简体   繁体   English

如何在覆盖后调用原始方法

[英]How to call original method after its override

In Javascript , when I override Date.prototype.toString function, the output of the Date() function is not affected and the function keeps its original code. In Javascript , when I override Date.prototype.toString function, the output of the Date() function is not affected and the function keeps its original code.

console.log(Date());
console.log(new Date().toString());

Date.prototype.toString = function() { return this.toISOString() }

console.log(Date());
console.log(new Date().toString());

"Tue Jan 26 2021 17:30:33 GMT-0500 (Eastern Standard Time)" “2021 年 1 月 26 日星期二 17:30:33 GMT-0500(东部标准时间)”

"Tue Jan 26 2021 17:30:33 GMT-0500 (Eastern Standard Time)" “2021 年 1 月 26 日星期二 17:30:33 GMT-0500(东部标准时间)”

"Tue Jan 26 2021 17:30:33 GMT-0500 (Eastern Standard Time)" “2021 年 1 月 26 日星期二 17:30:33 GMT-0500(东部标准时间)”

"2021-01-26T22:30:33.821Z" “2021-01-26T22:30:33.821Z”

Test it here .在这里测试一下。


However, when I needed to use a more complex override of the Date class - in my case changing the clock step to 5-second interval - after I override Date.prototype.toString function, the output of the Date() changes as well.但是,当我需要使用更复杂的Date class 覆盖时 - 在我的情况下将时钟步长更改为 5 秒间隔 - 在我覆盖Date.prototype.toString之后,Z78E6221F6393D14CE6 的Date() Since the functionality of the Date() function should not change, this is an unwanted and unwelcome change.由于Date() function 的功能不应更改,因此这是不受欢迎且不受欢迎的更改。

console.log(Date());
console.log(new Date().toString());

(function() {
  let ___now = Date.now;
  Date = new Proxy(Date, {
    construct(target, args) {
      if (args[0] === undefined) args[0] = this.adjust()
      let date = new target(...args);
      return date;
    },
    apply(target, thisArg, argumentList) {
      return new Date(this.adjust()).toString();
    },
    adjust() {
      return 5000 * Math.floor(___now() / 5000);
    }
  });
})();
Date.prototype.toString = function() { return this.toISOString() }

console.log(Date());
console.log(new Date().toString());

"Tue Jan 26 2021 17:30:35 GMT-0500 (Eastern Standard Time)" “2021 年 1 月 26 日星期二 17:30:35 GMT-0500(东部标准时间)”

"Tue Jan 26 2021 17:30:35 GMT-0500 (Eastern Standard Time)" “2021 年 1 月 26 日星期二 17:30:35 GMT-0500(东部标准时间)”

"2021-01-26T22:30:35.000Z" "2021-01-26T22:30:35.000Z" “2021-01-26T22:30:35.000Z” “2021-01-26T22:30:35.000Z”

"2021-01-26T22:30:35.000Z" "2021-01-26T22:30:35.000Z" “2021-01-26T22:30:35.000Z” “2021-01-26T22:30:35.000Z”

Test it here .在这里测试一下。


How should I modify the code above to force Date() function to use the original toString() function, even when it is overridden?我应该如何修改上面的代码以强制Date() function 使用原始的toString() function,即使它被覆盖? The change of toString() should be applicable only when it is called explicitly, as shown in the examples above. toString()的更改应该仅在显式调用时才适用,如上面的示例所示。

The following should work for you:以下内容应该适合您:

 console.log(Date()); console.log(new Date().toString()); (function() { const ___now = Date.now; const ___toString = Date.prototype.toString; Date = new Proxy(Date, { construct(target, args) { if (args[0] === undefined) args[0] = this.adjust() let date = new target(...args); return date; }, apply(target, thisArg, argumentList) { return ___toString.bind(new Date()).call(); }, adjust() { return 5000 * Math.floor(___now() / 5000); } }); })(); Date.prototype.toString = function() { return this.toISOString() } console.log(Date()); console.log(new Date().toString());

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

相关问题 ExtJS:如何从重写中调用原始方法? - ExtJS: How to call original method from an override? javascript对象可以覆盖其父方法并在该方法中调用它吗? - Can a javascript object override its parent method and call it in that method? 如何覆盖Javascript中的方法并将其应用于原始对象,而不是孩子? - How can I override a method in Javascript and apply it to the original object, not a child? 如何重写javascript方法并从类中调用? - How to override javascript method and call from class? 如何创建可以修改其原始函数调用的对象 - How to create an object that can modify its original function call 如何将覆盖的原型方法“重置”回其原始行为 - How to “reset” an overridden Prototype method back to its original behaviour 缩放画布后如何在画布中保存原始尺寸的图像 - How to save an image in its original size in a canvas after scaled the canvas 用javascript函数更改HTML表格后,如何将其重置为原始表格? - How to reset an HTML table to its original after changing it with a javascript function? 再次单击后如何将事件恢复为原始状态? - How to revert event to its original state after clicking on it again? 如何在交换后将列表元素返回到其原始样式 - How to return a list element to its original styles after a swap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM