variable step between functions with JQUERY

0

I'm having some doubts about the logic of passing variables between functions, the example is as follows:

having 3 functions (main function, secondary function and final function)

// mainfunction sends and retrieves values via ajax, calls functionfunctionalfunctional and funcionfinal

$('.input').on('click',function (){
//aqui obtengo el valor del DOM que usare para el primer envio de informacion via ajax con su respectiva respuesta
var unavariable = $(this).attr("id");
})
.done(function(data){
//con el data obtenido consulto un controlador diferente via ajax nuevamente genero un QUERY EN SQL y retorno la informacion
})
.done(function(data){
//asigno la informacion de la consulta en sql en un elemento <table>, botones y demas
//TAMBIEN HAGO LA LLAMADA A funcionsecundaria
funcionsecundaria();
funcionfinal(unavariable); //AQUI ESPERO ENVIAR "unavariable" y el RETURN de funcionsecundaria(otravaciable)
//funcionfinal(unavariable,otravariable)
});

function funcionsecundaria(){
//despues de obtener los resultados deseados, funcion1 manda a llamar funcionsecundaria
//aqui hago un barrido de elementos que necesito y creo un json de sus id
//ESTA FUNCIÓN RECIBE UNA ENTRADA DE VALORES MANUAL
var otravariable=JSON.stringify(a_r_r_a_y_s);

return otravariable;
}

function funcionfinal(){
//aqui espero obtener los dos valores (unavariable y otra variable) seran enviadas a un controlador en php y ocurrira la magia
//funcionfinal(unavariable,otravariable)
//TENIENDO EN CUENTA QUE ESTA FUNCION SERA DESENCADENADA POR funcionprincipal
}
    
asked by matteo 13.11.2018 в 18:42
source

1 answer

1

First of all you have to take into account the scope of the variables in JavaScript (what in English is called "scope"). JavaScript defines two areas for these, one global and one local.

Broadly speaking, if a variable is declared within a function, its scope will be within the block of that function, and outside of it, the variable does not exist;

function ejemplo() {
  var numero = 1;
}
//La variable numero fuera de la funcion no está definida
console.log(numero);

On the other hand, when you execute the secondary function inside the main function, you are not saving the value that returns that function, which as you indicate at the end, then you should receive the final function. To pass this variable to the final function, you must first save the value returned in a variable that is contained in the local scope where you will pass the value by parameters;

$('.input').on('click',function (){

var unavariable = $(this).attr("id");
}).done(function(data){
  //codigo
})
.done(function(data){
/*Aqui no guardas el valor devuelto por funcionsecundaria, con lo que al llamar la funcionfinal 
(que debe recibir dos parametros como indicas al final de tu script) no le estas pasando 
correctamente las variables. Deberia ser algo asi;*/

var variablesecundaria = funcionsecundaria(otravariable);
//otravariable guardara el valor que retorna la funcion

funcionfinal(unavariable, variablesecundaria);
});

function funcionsecundaria(){

var otravariable=JSON.stringify(a_r_r_a_y_s);
return otravariable;
}

function funcionfinal(unavariable, otravariable){
  //codigo
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

You can review something else about the scope of var in the mozilla documentation var - JavaScript | MDN . In addition, ECMAScript 6 introduces other types of statements such as those of const and let

    
answered by 13.11.2018 в 19:48