简体   繁体   English

如何将具有不同参数的相同JS函数存储在数组中并调用它们?

[英]How to store same JS function with different parameters in array and call them?

I need to use a function like this for multiple times with different ids based on different conditions (eg select values). 我需要根据不同的条件(例如选择值)使用类似的函数多次使用具有不同id的函数。 I don't want to write a different function for every id because there will be lots of them in the future. 我不想为每个id编写不同的函数,因为将来会有很多。

var getValue = function(id){

    var Element = document.getElementById(id);

    if(Element){
        if(Element.value){
            return " " + Element.value;
        }
        return "";
    }
    return "";
}

I have an array including some other functions: 我有一个包含其他功能的数组:

FunctionList = [ SomeFunction, SomeFunction2 ];

Based on some conditions, I add getValue function with different parameters to this array. 根据某些条件,我向此数组添加具有不同参数的getValue函数。

FunctionList.push(getValue(SomeId1));
FunctionList.push(getValue(SomeId2));
FunctionList.push(getValue(SomeOtherId));

In the end, I need to add outputs of my functions (which are strings) to another string. 最后,我需要将函数的输出(字符串)添加到另一个字符串。

var updateCode = function(){
    var code = CodeHeader;
    for (var i=0; i<FunctionList.length; i++){
        code += FunctionList[i]();
    } 
    return code;
}

But I am unable to use it like this since some items of array are not function names now. 但是我不能像这样使用它,因为数组的某些项现在不是函数名。 I like pure js solutions, but if it's not possible I can go for jQuery or so. 我喜欢纯js解决方案,但是如果不可能的话,我可以选择jQuery左右。

You can use bind to create such a tailored function on the fly: 您可以使用bind即时创建这样的定制功能:

FunctionList.push(getValue.bind(null, SomeId1));

The first argument ( null here) will act as this during function execution. 第一个参数(此处为null )将在函数执行期间充当this参数。

This differs from what you had: 这与您所拥有的不同:

FunctionList.push(getValue(SomeId1));

...as here you don't push a function, but the result of a function execution . ...如此处所示,您不是按功能,而是按功能执行结果 bind however, will not execute the function, but create a new one from the given function. 但是bind不会执行该功能,而是根据给定的功能创建一个新的功能。

Alternative 替代

In your example you call the same function with different arguments. 在您的示例中,您使用不同的参数调用相同的函数。 If that is always the case, you could also go for an array of only arguments: 如果总是这样,则还可以使用仅包含参数的数组:

argumentList.push(SomeId1); // Not the function, just the argument
// ...etc

And then updateCode would become: 然后updateCode将变为:

var updateCode = function(){
    var code = CodeHeader;
    for (var i=0; i<argumentList.length; i++){
        code += getValue(argumentList[i]);
    } 
    return code;
}

... or shorter by using map : ...或更短的map

var updateCode = () => CodeHeader + argumentList.map(getValue).join("");

As in your getValue function, you prefix the returned value with a space, you could omit that prefix, and just use .join(" ") in the above one-liner. 就像在getValue函数中一样,您为返回的值添加一个空格前缀,可以省略该前缀,而只需在上述单行代码中使用.join(" ") The downside is that it would not deal the same way with empty return values. 缺点是,对于空的返回值,它不会以相同的方式处理。

暂无
暂无

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

相关问题 如何将函数及其参数存储在数组中并顺序执行? - How do I store functions with their parameters in an array and execute them sequentially? Vue 3:如何在 Vuex 商店的同一个 mutations.js 中从另一个 function 调用 function - Vue 3: How to call function from another function in the same mutations.js in Vuex store 如何js调用function并访问调用者参数 - how to js call function and access caller parameters 如何通过导出导出功能相同但参数不同的功能 - How to export the same function but with different parameters by export angular-Js:无法访问同一控制器内不同函数调用中的数组元素 - angular-Js: not able to access array elements in different function call within same controller 如何在JavaScript中使用不同的参数从不同的地方调用相同的函数,并且仍然保留该函数的变量的旧值? - How to call same function from different places using different parameters in javascript and still retain old values of variables of that function? 使用参数数组调用函数 - Call function with array of parameters 如何在同一个函数中存储3个不同的结果 - How to store 3 different results within the same function JS 相同的 function 不同的参数在每个 object 值。 怎样才能少写? - JS same function different parameters in every object value. How can I write less? 如何以两种不同的方式调用同一函数? - How to call the same function in two different ways?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM