Search code examples
javascriptdateprototypejira

How to "reset" an overridden Prototype method back to its original behaviour


A script that I cannot control or remove has decided to override the default behaviour of the Date.prototype.setFullYear method. This causes problems for several libraries - such as date-fns - that our code relies heavily on.

Is there any way to "reset" the Date.prototype.setFullYear function back to its original behaviour? It does not matter whether the script that modified the prototype originally works afterwards or not - I don't need it to work, I just cannot remove it easily.

Update:

The script that causes problems is one of many bundled on the Jira Core 7.13.3 platform that loads on all pages. It is named calendar-lib, and causes issues when developing plugins on the platform.

Because calendar-lib is a part of the base Jira installation, we cannot control the loading order; the offending script will always load first - as far as I know.

Like @Jonas suggested it turns out that the native method is stored in a variable named Date.prototype.__msh_oldSetFullYear which perhaps could be used reset the global behaviour with @Haseeb's answer.

Update 17.06.19:

This issue has now been confirmed by the Jira developer team: https://jira.atlassian.com/browse/JRASERVER-69491

Solution:

As pointed out by @Jonas and @Rob in the comments, setting the following statement on the starting point of our application solved the issue:

Date.prototype.setFullYear = Date.prototype.__msh_oldSetFullYear;

Where Date.prototype.__msh_oldSetFullYear is the native implementation (luckily) stored and exposed by the offending calendar-lib code.

There were many really good suggestions in the comments below, so be sure to check those out if you are in a similar situation.

Thanks!


Solution

  • As pointed out by @Jonas and @Rob in the comments of the original question, setting the following statement on the starting point of our application solved the issue:

    Date.prototype.setFullYear = Date.prototype.__msh_oldSetFullYear;
    

    Where Date.prototype.__msh_oldSetFullYear is the native implementation of setFullYear that (luckily) had stored and exposed through a variable by the offending calendar-lib code.

    There were other really good suggestions in the comments below the original post as well - such as creating an iFrame and extracting it from there - so be sure to check those out if you are in a similar situation.