Return one of the functions in jquery.

1

Hello, is it possible to return one of these two functions that are inside another function separately? For example, depending on the situation I want to return the result of the function question and in another moment the function answer?

function psNF(){
    var pergunta = function(){
        alert('a');
    }
    var resposta = function(){
        alert('b');
    }
}
    
asked by anonymous 04.08.2017 / 14:17

2 answers

1

You can call the function by passing a certain parameter, so there is no need to create more than one function to call functions.

function psNF(opcao) {

  if (opcao == 1) {
    var pergunta = function() {
      alert('a');
    }
  } else {
    var resposta = function() {
      alert('b');
    }
  }
}

Remember that the parameter you set is you. You can pass int,string,char and several other types . You just have to stay logged in at the time of if condition to do the correct treatment.

And of course at the time of calling the function, pass the parameter you want.

    
04.08.2017 / 14:41
0

Only create a separate named function for each of them

function psNF(){
    var pergunta = function(){
        alert('a');
    }
}

function rpNF(){
    var resposta = function(){
        alert('b');
    }
}

Then assign the function call to where you are going to use each one.

<button type="button" name="pergunta" onclick="psNF();" />

<button type="button" name="resposta" onclick="rpNF();" />
    
04.08.2017 / 14:30