0

I have a java script function override that occurs when certain condition is met. How can I reset these functions to its original state if I don't need it anymore.

function setProductFields() {

  if (shouldShow == 'true') {

      selectField = function() {
        //override
      }
  } else {
     selectField() <--- //return to its original state
  }
}
3
  • What exactly do you want to override? Commented Mar 23, 2018 at 6:44
  • I've override the selectField() successfully. But I dont need it if shouldShow is false so I want it to revert to original Commented Mar 23, 2018 at 6:46
  • why do you want to override existing function ? can you explain the scenario. Commented Mar 23, 2018 at 6:57

1 Answer 1

1

Save a reference to the old selectField in a variable, prior to changing it:

var oldSelectField = selectField;

// ... other code ...

function setProductFields() {
  if (shouldShow == 'true') {
      selectField = function() {
        //override
      }
  } else {
     selectField = oldSelectField;
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. But it is still calling the selectField() inside the setProductFIelds() I've added an alert there
Oops all good now. I made a mistake calling the backup inside the setProductFields()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.