简体   繁体   English

将函数赋值给变量并传递参数

[英]Assign function to variable and pass parameter

I guess this is probably an easy problem, but I cannot figure it out myself (using the wrong search criteria?). 我想这可能是一个简单的问题,但我自己无法弄明白(使用错误的搜索条件?)。

As the title says, I want to assign a function to a variable and pass a variable to that function like this: 正如标题所说,我想为变量分配一个函数,并将变量传递给该函数,如下所示:

function doStuff(control) {
    var myObj = {
        "smooth": false,
        "mouseup": function(value) {
            jQuery(_getSelectorStringFromControlId(control.id)).blur();
        }
    };
}

Now I want the control.id to be replaced with its actual value before the function is assigned to the mouseup variable. 现在我希望在将函数分配给mouseup变量之前将control.id替换为其实际值。

So when control.id = 'myOwnId' then myObj.mouseup should be: 所以当control.id ='myOwnId'时, myObj.mouseup应该是:

function(value) {
    jQuery(_getSelectorStringFromControlId('myOwnId')).blur();
}

How can this be accomplished? 如何实现这一目标?

this is the basic form for assigning a function to a variable 这是将函数赋值给变量的基本形式

var myFunc = function(x) {
   console.log(x);
}

once that is done, you can invoke the function like so (passing in anything you want) 完成后,您可以像这样调用函数(传入您想要的任何内容)

myFunc(1);

You will have to call: 你必须打电话:

var some_control = {id: "myOwnId"};
doStuff(some_control);

It creates an object using object literals, { } . 它使用对象文字{ }创建一个对象。 Then, a property id is defined. 然后,定义属性id This whole object is passed as a parameter to function doStuff . 整个对象作为参数传递给函数doStuff

I think what you're looking for here is a closure, which is where a function which remembers variables that were in scope when it was created ("control" in your case) but then gets called from elsewhere. 我认为你在这里寻找的是一个闭包,它是一个函数,它记住创建时在范围内的变量(在你的情况下是“控制”),然后从其他地方调用。 In your example, you can return myObj from the doStuff function, and whenever you call mouseup() it will refer to the "right" value of control. 在您的示例中,您可以从doStuff函数返回myObj,每当调用mouseup()时,它将引用控件的“右”值。 So in fact, you're creating a closure in that example code already. 所以实际上,你已经在该示例代码中创建了一个闭包。

If you want to make the closure keep a reference to just the id, you could do this: 如果你想让闭包保持对id的引用,你可以这样做:

function doStuff(control) {
    var myId = control.id;
    var myObj = {
        "smooth": false,
        "mouseup": function(value) {
            jQuery(_getSelectorStringFromControlId(myId)).blur();
        }
    };
}

You can now have lots of copies of myObj floating round your page, and on each one, mouseup() will be using a different id. 您现在可以在页面上方放置大量myObj副本,并且每个副本上的mouseup()将使用不同的ID。

More info on closures: https://developer.mozilla.org/en/JavaScript/Guide/Closures 有关闭包的更多信息: https//developer.mozilla.org/en/JavaScript/Guide/Closures

As an aside, closures are the best thing ever for utterly astonishing people who've never done any functional programming. 顺便说一句,对于那些从未做过任何函数式编程的人来说,闭包是最好的事情

function doStuff(control) {
    var myObj = {
        "smooth": false,
        "mouseup": function(value) {
            jQuery(_getSelectorStringFromControlId(control.id)).blur();
         }
    };
}

doStuff({id: "yourID"}); // works now

Test it on jsFiddle . jsFiddle上测试它。

There's one problem left: You don't have access to the inner value parameter but I assume that you're going to bind this anonymous function to an event listener. 还有一个问题:您无法访问内部value参数,但我假设您要将此匿名函数绑定到事件侦听器。

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

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