覆盖Javascript Date构造函数? [英] Overriding the Javascript Date constructor?

查看:72
本文介绍了覆盖Javascript Date构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个对当前日期敏感的浏览器应用程序。

I am developing a browser application that is sensitive to the current date.

在我的应用程序代码中,我调用 new Date 并根据当前时间执行计算并相应地渲染视图。

Throughout my application's code, I call new Date and perform calculations based on the current time and render the view accordingly.

为了测试我的应用程序不同的潜在日历日,我将不得不经常将我的系统时钟更改为过去或将来,这是一个烦恼,可能对我的计算机不健康。

In order to test my application for different potential calendar days, I would have to constantly change my system clock to the past or future, which is an annoyance and probably not healthy for my computer.

纯粹出于测试目的(我绝不会使用此代码我决定通过在控制台中执行此操作来覆盖内置的 Date 构造函数:

So purely for testing purposes (I would never use this code in production), I decided to override the built-in Date constructor by doing this in the console:

// create a date object for this Friday:
var d = new Date(2012, 0, 20)
//override Date constructor so all newly constructed dates return this Friday
Date = function(){return d}

考虑到这一假设,我尝试过这个并得到了奇怪的结果:

With this assumption in mind, I tried this and got strange results:

var now = new Date
Sat Apr 07 2012 00:00:00 GMT-0400 (EDT)

now = new Date
Tue Jul 10 2012 00:00:00 GMT-0400 (EDT)

now = new Date
Wed Jul 09 2014 00:00:00 GMT-0400 (EDT)

now = new Date
Wed Jun 07 2023 00:00:00 GMT-0400 (EDT)

......依此类推......

...and so on....

我的问题是,这到底发生了什么?

My question is, what exactly is going on here?

如果我覆盖构造函数以返回静态日期,为什么它会给出不相关且不断递增的日期?

If I overrode the constructor to return a static date, why does it give unrelated and constantly incrementing dates?

另外,是否有一种有效的方法我可以覆盖Date构造函数以在将来返回一个静态日期,而不必在我的代码中经历所有日期实例化调用并修改输出吗?

Also, is there an effective way I can override the Date constructor to return a static date in the future without having to go through all date instantiation calls in my code and modifying the output?

提前感谢。

编辑:

我在一个新窗口中尝试了我的代码,它按预期工作。

I tried my code in a fresh window and it worked as expected.

似乎罪魁祸首是jQuery UI datepicker插件,它称之为refre sh方法。当我禁用它的调用时,日期覆盖正常工作,但是一旦我使用datepicker,就会发生上面的奇怪行为。

It seems the culprit was the jQuery UI datepicker plugin which was calling its "refresh" method. When I disable its call, the date overriding works normally, but as soon as I use the datepicker, the strange behavior above occurs.

不知道为什么这个流行的插件会以某种方式影响像这样的全球性事物。如果有人有任何想法,请告诉我。

No idea why this popular plugin would somehow affect something global like this. If anyone has any ideas, let me know.

很抱歉没有弄清楚真正的罪魁祸首。

Sorry for not figuring out the true culprit earlier.

推荐答案

我测试了你的代码:

// create a date object for this Friday:
var d = new Date(2012, 0, 20);
//override Date constructor so all newly constructed dates return this Friday
Date = function(){return d;};

var now = new Date()
console.log(now);

now = new Date()
console.log(now);

now = new Date()
console.log(now);

now = new Date()
console.log(now);

结果????为何如此不同?

And the result ???? Why so different?

Date {Fri Jan 20 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}
Date {Fri Jan 20 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}
Date {Fri Jan 20 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}
Date {Fri Jan 20 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}






编辑:


我看到,只要您与日期选择器进行交互,行为就会有所不同。尝试另一个测试,更改现在类似于与日期选择器交互:

I saw that whenever you interact with the Date Picker, the behavior goes different. Try another test, change the now is something like interact with Date Picker:

// create a date object for this Friday:
var d = new Date(2012, 0, 20);
//override Date constructor so all newly constructed dates return this Friday
Date = function(){return d;};

var now = new Date();
var another = new Date();
console.log(now);

another.setDate(13);

now = new Date()
console.log(now);

结果是:

Date {Fri Jan 20 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}
Date {Fri Jan 13 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}

那么,出了什么问题?
您已经覆盖核心日期函数

So, what goes wrong? You already overridden core Date function by

Date = function(){return d;}; // after construction, all date will be d (2012-01-20)
var now = new Date(); // you instantiate a date, but actually now variable is d (2012-01-20)
var another = new Date(); // you instantiate a date, but another is still d (2012-01-20)
another.setDate(13); // change another date to 13 is to change now to 13 (because now and another is still one d)

now = new Date() // still d
console.log(now); // print out now (2012-01-13)

所以,你用一个覆盖核心日期函数导致所有日期使用相同(仅一个)实例的函数,即d(2012-01-20)。更改任何影响他人的日期。

So, you overrides core Date function by a function that causes all date use the same (just one) instance, which is d (2012-01-20). Change any dates affect others.

这篇关于覆盖Javascript Date构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆