在 Javascript 中,当我覆盖 Date.prototype.toString
函数时,Date()
函数的输出不受影响并且函数保留其原始代码。
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)"
"Tue Jan 26 2021 17:30:33 GMT-0500 (Eastern Standard Time)"
"Tue Jan 26 2021 17:30:33 GMT-0500 (Eastern Standard Time)"
"2021-01-26T22:30:33.821Z"
测试一下 here .
但是,当我需要使用更复杂的 Date
类覆盖时 - 在我的例子中将时钟步长更改为 5 秒间隔 - 在我覆盖 Date.prototype.toString 之后
函数,Date()
的输出也会发生变化。由于 Date()
函数的功能不应更改,因此这是
一个不需要的和不受欢迎的变化。
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)"
"Tue Jan 26 2021 17:30:35 GMT-0500 (Eastern Standard Time)"
"2021-01-26T22:30:35.000Z" "2021-01-26T22:30:35.000Z"
"2021-01-26T22:30:35.000Z" "2021-01-26T22:30:35.000Z"
测试一下 here .
我应该如何修改上面的代码以强制 Date()
函数使用原始的 toString()
函数,即使它被覆盖了? toString()
的更改应该仅在显式调用时才适用,如上例所示。
最佳答案
以下应该适合您:
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());
关于javascript - 如何在覆盖后调用原始方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65910344/