简体   繁体   English

如何将实函数分配给javascript对象以用作回调?

[英]How do I assign a real function into a javascript object to use as a callback?

I've got a Javascript mess that looks something like: 我的Javascript混乱看起来像:

Note: Not working code, just trying to illustrate my problem... 注意:没有工作代码,只是试图说明我的问题...

$(function() {
    $(foo).Something( {  //Something is a grid control
       buttons: {
            add: {
                onClick: function() { //Build dialog box to add stuff to grid
                    $('<div></div>')
                    .html('...')
                    .dialog({
                        buttons: {
                            done: { //Finished button on dialog box
                                OnClick: function() {
                                    $(this).dialog('close');
                                }
                            }
                        }
                    });
                }
            }
       } 
    } );
});

I'd like to replace some of the function(){...} s with real functions, to clean things up a bit and to get rid of all that indenting. 我想用实际函数替换一些function(){...} ,以清理一些东西并消除所有缩进。 How do I assign a real function to one of the callbacks rather than an anonymous function? 如何为回调之一分配实函数而不是匿名函数?

function abc(){
  return false;
}
var callback = {click : abc}

You can then use callback.click.call or callback.click.apply 然后,您可以使用callback.click.callcallback.click.apply

给出函数名称而不是function(){ ... }

+1 because of the awesome indentation example. 由于缩进示例很棒,因此+1。

You can define named functions as Joyce stated; 您可以按照Joyce所述定义命名函数; you can also considerably clean up that deeply nested code by just declaring some variables (function or not) and spreading the code into multiple, non-nested statements. 您还可以通过声明一些变量(无论是否具有功能)并将代码分散到多个非嵌套的语句中,从而大大清除深层嵌套的代码。

The dialog creation would be my first candidate for that type of refactoring (and it demonstrates uses a named function): 对话框的创建将是我首选的那种重构类型(并演示使用命名函数):

function CreateDialog() { //Build dialog box to add stuff to grid
                    $('<div></div>')
                    .html('...')
                    .dialog({
                        buttons: {
                            done: { //Finished button on dialog box
                                OnClick: function() {
                                    $(this).dialog('close');
                                }
                            }
                        }


$(function() {
    $(foo).Something( {  //Something is a grid control
       buttons: {
            add: {
                onClick: CreateDialog
                }
            }
       } 
    } );
});

I should also note that you can initialize most jQuery objects (looks like you are using jQueryUI) after they are created. 我还应该注意,在创建大多数jQuery对象(看起来像您正在使用jQueryUI)之后,您就可以对其进行初始化。 For example, you don't have to configure the buttons all in one shot: http://jqueryui.com/demos/dialog/#option-buttons 例如,您不必一次完成所有按钮的配置: http : //jqueryui.com/demos/dialog/#option-buttons

OK then, here goes my answer: 好吧,这就是我的答案:

Oops.. You did functionname() (note the extra ()s). 糟糕,您做了functionname() (请注意额外的()s)。 :sigh: That's what you get for working on this at 3 AM. :sigh:这就是您在凌晨3点进行此工作所获得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM