closures - Javascript: Calling a method stored in a variable - Stack Overflow

I am trying to take advantage of Javascript closures by creating a method in one context and storing it

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...!

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...!

Share Improve this question asked Nov 22, 2010 at 19:26 tpowtpow 7,90411 gold badges62 silver badges85 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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

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);
>>> var revertEvent; 
undefined
>>> (function() {   revertEvent = function() { alert('hi') }   })();
undefined
>>> revertEvent()

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745047992a4608212.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信