简体   繁体   中英

How to use an argument from one function to call another function

I'm trying to figure out how to pass an argument to one function, which is actually a second function. Hard to explain so here is some sample code:

keyDownEvent("#thisID",nameOfFunctionToRunOnOpen);

function keyDownEvent(id, openFunction) {
$(id).dialog( {
    close: focusOnInput(),
    open: nameOfFunctionToRunOnOpen,
    modal: true,
    show: "clip",
    hide: "fade"
});
}

Everything else is standard for my .dialog() calls, just want to modify what happens on Open. Any idea how this can be achieved?

Just use an anonymous function:

function keyDownEvent(id, openFunction) {
$(id).dialog( {
    close: focusOnInput(),
    open: function(){openFunction(someArgument);},
    modal: true,
    show: "clip",
    hide: "fade"
});
}

应该给您的第二个参数一个实际的函数对象,而不仅仅是字符串中函数的名称。

You can simply pass the function to your second function. Functions are objects in JavaScript and can be saved in variables:

function outer(someOtherFunction) {
    someOtherFunction();
}

outer(function () {
    alert('inner function');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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