Arguments to JavaScript Anonymous Function

前端 未结 5 864
悲&欢浪女
悲&欢浪女 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-13 11:56

    How about a closure:

    for (var i = 0; i < somearray.length; i++) {
        var val = somearray[i][0];
        myclass.foo({'arg1': val}, function(v) {
          return function() {console.log(v) };
        }(val) );
    }
    

提交回复
热议问题