Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在重写后调用原始方法_Javascript - Fatal编程技术网

Javascript 如何在重写后调用原始方法

Javascript 如何在重写后调用原始方法,javascript,Javascript,在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()

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());
“2021年1月26日星期二17:30:33 GMT-0500(东部标准时间)”

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

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

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

测试一下


然而,当我需要在重写
Date.prototype.toString
函数后,使用更复杂的
Date
类重写时(在我的例子中,将时钟步长更改为5秒间隔),
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());
“2021年1月26日星期二17:30:35 GMT-0500(东部标准时间)”

“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”

测试一下



我应该如何修改上面的代码以强制
Date()
函数使用原始的
toString()
函数,即使它被重写了?
toString()
的更改应仅在显式调用时适用,如上述示例所示。

以下内容适用于您:

console.log(Date());
log(newdate().toString());
(功能(){
const\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu=Date.now;
const\uuuuuuu toString=Date.prototype.toString;
日期=新代理(日期{
构造(目标,args){
如果(args[0]==未定义)args[0]=此.adjust()
let date=新目标(…args);
返回日期;
},
应用(目标、此参数、参数列表){
return uuu_uutoString.bind(new Date()).call();
},
调整{
返回5000*Math.floor(\uuuuu now()/5000);
}
});
})();
Date.prototype.toString=function(){返回this.toISOString()}
console.log(Date());
log(newdate().toString())