Can you pass the function name as parameter?

Asked

Viewed 3,211 times

2

I’m racking my brain here for a solution whose is to pass function names as parameters to be executed.

I have the function ShowModal in which its goal is to call other functions and objects. How can the name of the function to execute it.

    $.showModal = function (idModal,idInputHidden,idNome,nameFunction,idOptional) {
    $(idModal).modal('show');
    if(idOptional == 'null'){
        $.setField(idInputHidden,idNome,idOptional);
    }else{
        $.nameFunction(idInputHidden,idNome);
    }
};

// Exemplo de uso Normal dela
$.showModal($('#myModalProduto'),$('#produto_modal_ctrl_id_key'),$('#produto_modal_ctrl_id'),'setFieldModalProduto',null);

//No Modal do Produto tenho a seguinte função setFieldModalProduto
$.setFieldModalProduto = function (a,b) {
            field1 =  $(a);
            field2 =  $(b);
        };
  • For me I’m not very clear.

  • 1

    Pass the function itself on the call, much more practical.

1 answer

1


Because they are first class objects, functions can be object properties or even arrays.

If you have not defined who the function belongs to (object or other variable) it will belong to the object window:

function chamaOutra(nome_funcao, parametro){
  window[nome_funcao](parametro);
}

function myAlert(parametro){
   alert(parametro);
}

chamaOutra("myAlert", "Teste 1");

If you have defined who it belongs to, for example, a variable to group the functions, just enter the variable "parent" before the function:

   

var chamaOutra = function (nome_pai, nome_funcao, parametro){
  window[nome_pai][nome_funcao](parametro);
}

var mensagens = {}
mensagens.myAlert = function (parametro){
      alert(parametro);
}

chamaOutra("mensagens", "myAlert", "Teste 2");

In this answer, found a function that I believe may be interesting to your case:

// lista com suas funções
var mensagens = {}
mensagens.myAlert = function (parametro){
      alert(parametro);
}

// função para chamar outras
function executeFunctionByName(functionName, context /*, args */) {
  var args = [].slice.call(arguments).splice(2);
  var namespaces = functionName.split(".");
  var func = namespaces.pop();
  for(var i = 0; i < namespaces.length; i++) {
    context = context[namespaces[i]];
  }
  return context[func].apply(context, args);
}

// chamando outra função
executeFunctionByName("mensagens.myAlert", window, "Teste 3");

As commented by @bfavaretto, you can also pass as argument, but in this case you cannot use a string as parameter:

// lista de funções 
var  myAlert = function(my_parametro){
   alert(my_parametro);
}

// função para chamar outras
function chamaOutras(funcao, parametro){
    if(typeof(funcao)=="function"){
        funcao.call();
    }
}

// chamando outras
chamaOutras(myAlert("Teste 4"));

  • 2

    Because they are first-class objects, they can also be passed as arguments. What is preferable to passing the name.

  • I’ll check on flw!

  • Worked the above answer?

  • Yes, I’ve already marked you as answered. Thanks again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.