JavaScript: Is it possible to pass a variable into a callback function that is assigned to a variable?

风格不统一 提交于 2019-12-17 19:57:35

问题


A lot of people say that this is asked too much in the comments, which made me hesitant to ask this, but I still have not found a solution in their answers, mostly because (1) they are typically using jQuery and (2) the questions usually contain technicalities I do not understand.

I have a function with a variable inside. The variable is assigned a function. I'm sure this concept is not exclusive to AJAX, but that is the context I am using it in, if it makes a difference.

function iClick(this)
{
    var foo = "I would like to pass this.";

    ajax.onreadystatechange = function (foo) { alert(foo); }
}

I want to pass a variable into the function. However, since there is no original function declaration, how do I specify parameters? Can I even do that?


回答1:


Just don't declare that variable as a parameter in your anonymous function, like this:

function iClick(this)
{
    var foo = "I would like to pass this.";
    ajax.onreadystatechange = function () { alert(foo); }
}

When you call the first parameter foo it's whatever's calling that callback passes in that's foo inside the function. If you want to reference a previously declared variable just do that, make sure not to use a parameter with the same name.




回答2:


You can create a function like this

var c="hello";

(function(b){
   alert(b) 
})(c);

result would be "hello"




回答3:


You can also do this, but maybe it's not necessary:

function iClick(this)
{
    var foo = "I would like to pass this.";

    ajax.onreadystatechange = (function(thevar) {
        return function () { alert(thevar); };
      })(foo);
}



回答4:


As @John Hartsock referred, the answer that everyone should really remember is this

var c="hello";

(function(b){
   alert(b) 
})(c);

And that's very important for example in a for loop when there is some async function inside it, because otherwise you don't get the correct item.

Tell me, what comes out from here?

for (var i=0; i<5; i++){
  setTimeout(function(){ 
    console.log(i); 
    }, 1000);
}

Exactly: all 5, because when all the timers are triggered after 1 second, variable i is already at the value 5.

But if you use a self-invoked anonymous function (SIAF) like this

for (var i=0; i<5; i++){
  (function (j){
    setTimeout(function(){ 
      console.log(j); 
      }, 1000);
   })(i);
}

it does work, since every time the function is evoked, it runs another instance of the function and as any function, it has its own local variables. I do not merely define the function, I also run it right away (through the (); at the end), but then internally a new instance of the function will be created with different internal local variables, as I parse to the function a different variable every time I run it.




回答5:


I belive you wanted something like that

function handleAjaxRequest(params) {
    var context = {'b':'inner', 'c': params['c']};
    function rendered(html) {
      // render
    }
    function gotPart(part) {
        context['a'] = part;
        engine.render(context).addCallback(rendered);
    }
    ajax.getPart(params).addCallback(gotPart);
}


来源:https://stackoverflow.com/questions/3946177/javascript-is-it-possible-to-pass-a-variable-into-a-callback-function-that-is-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!