简体   繁体   English

外部变量作用域在javascript函数内部不可用

[英]Outer Variable scope is not available inside the javascript function

How to access the outer variable inside the inner javascript function , please check my code below i need to access "index" variable inside app() function . 如何访问内部javascript函数内部的外部变量,请在下面检查我的代码,我需要访问app()函数内部的“索引”变量。

 function validate()
    {

    for (index = 0; index < results.length; index++) {
        var parsedData_1;
        var provider = results[index].get("provider");                          
        var user = results[index].get("user");          
        var addresses = user.get("addresses");

        var address = addresses[1];

        var GameScore = Parse.Object.extend("Address");
        var query = new Parse.Query(GameScore);         
        var data = JSON.stringify(address);     


        var parsedData = jQuery.parseJSON(data);            
        query.get(parsedData.objectId, {
        success: function(resultadr) {          

        var res = JSON.stringify(resultadr);
        var parsedData_1 = jQuery.parseJSON(res);   
        var apartment = parsedData_1.apartment;

        apa(apartment);

        },
        error: function(object, error) {            
        alert('error');             

        }
        });         

        function apa(apartment)
        {
        alert(index); [I'm not getting the index value inside this function]
        alert(apartment);

        }

    }

    }

Inside the function apa() i need the index count , but im unable to get it , please help how to access the outer value inside the function. 在函数apa()中,我需要索引计数,但是我无法获取它,请帮助如何访问函数中的外部值。

据我所知apa()无法访问index变量,所以我将其放在函数调用的参数中,然后像这样调用apa()

apa(apartment, index);

The problem is that you are using closures to access the index, thus the lastest value assinged to that variable is accessible in the app function. 问题在于您正在使用闭包来访问索引,因此可以在app函数中访问分配给该变量的最新值。 For example, if results.length equlas 11, index in app function will always be 11. 例如,如果results.length等于11,则应用程序函数中的索引将始终为11。

The soltion is to declare a variable inside the for loop, assign the index value to that variable, and pass the variable to app function. 解决的办法是在for循环中声明一个变量,将索引值分配给该变量,然后将该变量传递给app函数。

//Create a factory function for the app function
//Outside the for loop.

var createAppFunc = function (i) { 
    return function () { 
        console.log(i); 
    };
};
for (var index = 0, l = results.length; index < l; index++)
{
    var newVar = index;
    var app = createAppFunc(index); //Now, app is a function.
}

Good Luck :). 祝好运 :)。

You can use Function.bind() to pass the actual index as first argument to the sucess-callback. 您可以使用Function.bind()将实际索引作为第一个参数传递给成功回调。 There you can get it and pass it along as parameter to function apa() . 在那里,您可以获取它并将其作为参数传递给apa()函数。 And: use keyword var for the index in for() . 并且:使用关键字var作为for()的索引。

function validate() {

    for (var index = 0; index < results.length; index++) {

        /* all the vars here */

        query.get(parsedData.objectId, {
            success: function(idx, resultadr) {
                var res = JSON.stringify(resultadr);
                var parsedData_1 = jQuery.parseJSON(res);   
                var apartment = parsedData_1.apartment;
                apa(idx, apartment);
            }.bind(null, index),
            error: function(object, error) {            
               alert('error');             
            }
        });

        function apa(index, apartment) {
            console.log(index); console.log(apartment);
        }

    }
}

The first parameter (here: null) of bind() sets the context of the bound function. bind()的第一个参数(此处为null bind()设置绑定函数的context Whatever you pass in will be accessible in the success-callback by the keyword this . 无论您传入什么内容,都可以通过关键字this在成功回调中进行访问。

我认为每次调用该函数时,下一个循环调用都会损害该调用。因此,最后该方法将仅被调用一次。

您可以全局声明索引变量,然后所有函数都可以访问它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM