Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

javascript - 重写Javascript Date构造函数?(Overriding the Javascript Date constructor?)

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

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

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

(在整个应用程序代码中,我调用new Date并根据当前时间执行计算,并相应地呈现视图。)

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.

(为了测试我的应用程序在不同的潜在日历日中,我将不得不不断地将系统时钟更改为过去或将来,这很烦人,并且可能对我的计算机不利。)

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:

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

// 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?

(如果我重写了构造函数以返回静态日期,为什么它会给出不相关且不断增加的日期?)

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?

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

Thanks in advance.

(提前致谢。)

EDIT:

(编辑:)

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

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

It seems the culprit was the jQuery UI datepicker plugin which was calling its "refresh" method.

(罪魁祸首似乎是jQuery UI datepicker插件,它正在调用其“ refresh”方法。)

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.

(对不起,您没有早点弄清真正的罪魁祸首。)

  ask by Dan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...