JavaScript:是否可能将变量传递到分配给变量的回调函数中?

时间:2022-12-08 20:25:39

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.

很多人说这是问太多的评论,这使我犹豫问这个,但我仍然没有找到一个解决方案在他们的答案,主要是因为(1)他们通常使用jQuery和(2)通常包含技术我不懂的问题。

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.

里面有一个变量的函数。变量被分配一个函数。我确信这一概念并不是AJAX独有的,但这正是我使用它的上下文,如果它有什么不同的话。

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?

我想把一个变量传递给函数。但是,由于没有原始的函数声明,如何指定参数?我能做到吗?

5 个解决方案

#1


20  

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.

当你调用第一个参数foo时它是调用那个回调传入的东西那是函数内部的foo。如果您想引用先前声明的变量,那么请确保不要使用同名的参数。

#2


15  

You can create a function like this

可以创建这样的函数

var c="hello";

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

result would be "hello"

结果将是“你好”

#3


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


1  

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

正如@John Hartsock所说,每个人都应该记住的答案是

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.

这在for循环中非常重要当有一些async函数在里面时,否则你得不到正确的项。

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.

完全正确:所有5个,因为当所有计时器在1秒后被触发时,变量i已经在值5。

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

但是如果您使用自调用的匿名函数(SIAF),就像这样。

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


0  

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);
}

#1


20  

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.

当你调用第一个参数foo时它是调用那个回调传入的东西那是函数内部的foo。如果您想引用先前声明的变量,那么请确保不要使用同名的参数。

#2


15  

You can create a function like this

可以创建这样的函数

var c="hello";

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

result would be "hello"

结果将是“你好”

#3


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


1  

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

正如@John Hartsock所说,每个人都应该记住的答案是

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.

这在for循环中非常重要当有一些async函数在里面时,否则你得不到正确的项。

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.

完全正确:所有5个,因为当所有计时器在1秒后被触发时,变量i已经在值5。

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

但是如果您使用自调用的匿名函数(SIAF),就像这样。

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


0  

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);
}