Skip to content Skip to sidebar Skip to footer

How To Cache The Javascript Date Object?

I am using Date.js to support many cultures in my web app. The problem here is that date.js has code like this. Date.prototype._toString = Date.prototype.toString; Date.prototy

Solution 1:

Instead of including both date.js and date-fr-FR.js, you only need to include the fr-FR.js file to change the culture, which you will find in the src/globalization folder in the Datejs-all-Alpha1.zip file. The fr-FR.js file only contains the culture specific data, and it should override what is already included in date.js, without redefining the functionality.

Solution 2:

All you have to do is check whether _toString has been defined or not.

Date.prototype._toString = Date.prototype._toString || Date.prototype.toString;
Date.prototype.toString = function() {
    //doing something      returnthis._toString();
}

Solution 3:

You should only copy the function once:

Date.prototype._toString = Date.prototype.toString;

The second time you do this, it will copy the native toString function again, wich will then call itself via a recursive loop.

I don’t know if you are actually doing this more than once, bu in the fr-FR.js file there are no other toString methods defined so I’m guessing you are adding it manually.

Update

If you are including the date.js core twice (which you shouldn’t, just include the locales), you could probably check if the function is present first:

if ( typeofDate.prototype._toString != 'function' ) {
    // include date.js
}

Post a Comment for "How To Cache The Javascript Date Object?"