Javascript:调用存储在变量中的方法

发布于 2024-10-04 04:34:12 字数 748 浏览 1 评论 0原文

我试图通过在一个上下文中创建一个方法并将其存储在全局变量中来利用 Javascript 闭包,以便稍后可以从另一个上下文中调用它。

//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);
            }          
  }

因此,“createRevertFunction”保存原始对象的状态。调用此函数后,该对象“eventToBeReverted”会被修改,因此这提供了一种无需刷新页面即可将原始内容恢复到 UI 的方法。

我的问题是我似乎无法调用变量“revertEvent”中的函数。

我已经尝试过:

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

但都不起作用。任何帮助将不胜感激...!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何必那么矫情 2024-10-11 04:34:12

我认为这里最可能的问题是您在通过 createRevertFunction 设置之前尝试调用 revertEvent。要验证此更改,请更改 revertEvent 的声明,如下所示。

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

如果在 createRevertFunction 之前调用,则会弹出“尚未看到”警报。

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

就是爱搞怪 2024-10-11 04:34:12

使 createRevertFunction 返回内部函数。分配。

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);

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);
柠檬色的秋千 2024-10-11 04:34:12
>>> var revertEvent; 
undefined
>>> (function() {   revertEvent = function() { alert('hi') }   })();
undefined
>>> revertEvent()

这对我有用。向我们展示您对函数 createRevertFunction 的用法...

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

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

念﹏祤嫣 2024-10-11 04:34:12

revertEvent 仅在函数内部定义,之后就会被忘记。您必须将其保存到全局变量(在函数外部定义),或者返回创建的函数并存储它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文