Arguments to JavaScript Anonymous Function

前端 未结 5 863
悲&欢浪女
悲&欢浪女 2021-02-13 11:47
for (var i = 0; i < somearray.length; i++)
{
    myclass.foo({\'arg1\':somearray[i][0]}, function()
    {
        console.log(somearray[i][0]);
    });
}
5条回答
  •  野的像风
    2021-02-13 12:18

    You can pass variables values to annoymous function by using callback, something like

    myclass.foo(function(variable){
          return function(){
            console.log(variable);
          }
        })(variableValue);
    );
    

    check this post: https://shahpritesh.wordpress.com/2013/09/06/javascript-function-in-loop-passing-dynamic-variable-value/

提交回复
热议问题