Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.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对象的包装器,这样每当我有var a=new Date()之类的东西时,我想在构造函数中执行一些特定的代码 我基本上希望拥有自己的Date类,每当调用Date()而不是本机代码时,都需要调用该类。您需要保存本机Date对象的引用,而不是创建自己的包装器,该包装器调用本机Date,然后对其进行变异,或添加其他行为 var OldDate = Date; var Date = function() { var that = new OldDate()

我想要一种JavaScript中默认的
Date
对象的包装器,这样每当我有
var a=new Date()之类的东西时,我想在构造函数中执行一些特定的代码


我基本上希望拥有自己的Date类,每当调用
Date()
而不是本机代码时,都需要调用该类。

您需要保存本机
Date
对象的引用,而不是创建自己的包装器,该包装器调用本机
Date
,然后对其进行变异,或添加其他行为

var OldDate = Date;
var Date = function() {
  var that = new OldDate();
  that.mystuff = 5;
  // do other things with the date
  // and execute your own things
  // ...
  return that;
}

var now = new Date();
alert(now.mystuff);
但是,我不会弄乱本机对象