2

I am trying to take advantage of Javascript closures by creating a method in one context and storing it in a global variable so that it may be called later from another context.

//Global variable to hold the function
var revertEvent;

//creates the function and assigns it to the global variable
 function createRevertFunction(eventToBeReverted) {

            revertEvent = function () {
                alert("Now Restoring Event");
                $('#calendar').fullCalendar('renderEvent', eventToBeReverted, true);
            }          
  }

So, the "createRevertFunction" holds the state of the original object. That object "eventToBeReverted" is modified down the road after this function is called, so this provides a means to restore the original to the UI without page refresh.

My problem is that I can't seem to call the function in the variable "revertEvent".

I've tried:

revertEvent();
revertEvent.call();
window[revertEvent]();

and none of them work. Any help would be appreciated...!

4 Answers 4

2

I think the most likely problem here is that you're attempting to call revertEvent before it's been set via createRevertFunction. To verify this change the declaration of revertEvent as follows

var revertEvent = function() { alert('not set yet'); }

This will pop up the alert "not seet yet" in the case it's called before createRevertFunction

Sign up to request clarification or add additional context in comments.

1 Comment

I would also think that this is kind of his problem. @cinqoTimo when exactly in you code do you try to call the revertEvent function and when do you call createRevertFunction?
2

make createRevertFunction return the interior function. Assign.

var revertEvent;

//returns the function so we can do whatever with it
 function createRevertFunction(eventToBeReverted) {

        return function () {
            alert("Now Restoring Event");
            $('#calendar').fullCalendar('renderEvent', eventToBeReverted, true);
        }          
  }


revertEvent = createRevertFunction(e);

Comments

1
>>> var revertEvent; 
undefined
>>> (function() {   revertEvent = function() { alert('hi') }   })();
undefined
>>> revertEvent()

This works for me. Show us your usage of the function, createRevertFunction...

Comments

0

revertEvent is defined only inside your function and is forgotten afterwards. You will have to save it to a global variable (that is defined outside the function) or rather return the created function and store it.

3 Comments

revertEvent isn't defined in a function, and should have global scope.
revertEvent isn't local scope variable in this example.
Lol sorry you're right guys, didnt read well enough. Odd then.. will edit when found answer

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.