[英]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”
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”
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.