简体   繁体   English

修改回调函数的参数数量-javascript

[英]Modifying number of arguments for callback functions - javascript

I know many of you already used JavaScript UI widget plugins, etc... that offers callback functions. 我知道你们中许多人已经使用过JavaScript UI小部件插件等...,它们提供了回调函数。 For instance we have Object x and it has a function, let say .doThisAfterAnEvent() . 例如,我们有Object x并且它具有一个函数,比如说.doThisAfterAnEvent() And according to the official documentation of Object x , that function accepts a single parameter of type function() with one argument, let say _args . 根据Object x的官方文档,该函数接受带有一个参数的function()类型的单个参数,假设_args

To visualize, here is the example: 为了可视化,下面是示例:

var handler = function(_args) {
    // Do something.
}

var x = $("#element-to-widget-ify").transform()
x.doThisAfterAnEvent(handler)

My question is, how can I modify the method .doThisAfterAnEvent() to accept a function with two or more parameters instead of one? 我的问题是,如何修改方法.doThisAfterAnEvent()以接受具有两个或多个参数而不是一个的函数? In this case, I need to pass a second extra value to the handler function. 在这种情况下,我需要向handler函数传递第二个额外的值。


Edit: 编辑:

var widgets = {
    "widget-at-the-nav": $("#nav-widget").transform(),
    "widget-at-the-footer": $("#nav-footer").transform(),
    "widget-at-the-search": $("#nav-search").transform(),
    length: 3
}

var handler = function(_args, _option) {
    console.log("key in: " + _option
    // Other processes...
}

for(key in widgets) {
    console.log("key outer: " + key)
    widget[key].doThisAfterAnEvent(function(json) {
        console.log("key out: " + key)
        handler(json, key)
    })
}

This is my attempt. 这是我的尝试。 But it prints like this: 但它的打印结果如下:

key outer: widget-at-the-nav
key outer: widget-at-the-footer
key outer: widget-at-the-search
key out: widget-at-the-nav
key in: widget-at-the-nav
key out: widget-at-the-nav
key in: widget-at-the-nav
key out: widget-at-the-nav
key in: widget-at-the-nav

And I forgot to tell you guys that the function .doThisAfterAnEvent() (not the handler() function) has an AJAX call inside. 而且我忘了告诉大家,函数.doThisAfterAnEvent() (不是handler()函数)内部具有AJAX调用。

If you ask that, I guess you mean at the moment you call doThisAfterAnEvent, you already know one parameter over two for your handler. 如果您这样询问,我想您的意思是,在调用doThisAfterAnEvent的那一刻,您已经知道处理程序的一个参数超过两个。

In this case, the solution is too wrap your handler with two parameter in a anonymous function that only take one parameter and then call back your handler : 在这种情况下,解决方案是将您的处理程序带有两个参数的包装到一个匿名函数中,该函数仅接受一个参数,然后回调您的处理程序:

x.doThisAfterAnEvent(function(_arg) { handler(myKnownArg, _arg) });

This question is a mess, so I'm only going to touch on the most recent edit, and the code it contains. 这个问题是一团糟,所以我只涉及最新的编辑及其包含的代码。

Your approach with masking the handler with an anonymous function is pretty much correct, it's just that your loop is messing up your lexical scope. 您使用匿名函数屏蔽处理程序的方法是非常正确的,只是您的循环弄乱了您的词法范围。 The AJAX bit is a very important detail, because any AJAX calls are most likely going to operate long after you've looped, which means those callback functions will all reference the same final value of key . AJAX位是一个非常重要的细节,因为任何AJAX调用都很可能在循环后很长时间才能运行,这意味着这些回调函数都将引用相同的key最终值。

You need to create a new scope where the key is 'locked in', so that the references are correct. 您需要创建一个新的作用域,在该作用域中,键被“锁定”,以便引用正确。

 function Con () {} Con.prototype.doThisAfterAnEvent = function (fn) { // Fake some AJAX programming window.setTimeout(function () { fn.call(this); }, 1500); }; $.fn.transform = function () { return new Con(); }; var widgets = { "widget-at-the-nav": $("#nav-widget").transform(), "widget-at-the-footer": $("#nav-footer").transform(), "widget-at-the-search": $("#nav-search").transform() }; var handler = function(_args, _option) { console.log("key in: " + _option); // Other processes... }; for (var key in widgets) { console.log("key outer: " + key); (function (key) { // Use an IIFE to establish a new lexical scope widgets[key].doThisAfterAnEvent(function(json) { console.log("key out: " + key); handler(json, key); }); }(key)); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

In ES6 , we'd use let . ES6 ,我们将使用let

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

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