Javascript 访问javascript中函数内的函数内的变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39679212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:48:00  来源:igfitidea点击:

Access a variable inside a function which is inside a function in javascript?

javascriptfunctionvariablesreturnreturn-value

提问by Syam Ps

How can I access a variable inside a function which is inside a function in javascript ?

如何访问javascript中函数内的函数内的变量?

var a;
var surveyObjects = Parse.Object.extend(surveyObject);
var query= new Parse.Query(surveyObjects);
query.count({
    success: function(count){a = count;},
    error: function(error){}
});
alert("count of function "+a);

ais showing undefinedvalue. I need to use the value of aoutside.

a正在显示undefined价值。我需要使用a外部的值。

采纳答案by Syam Ps

Thanks. I declared the variable as global and inserted value inside the inner function and then made a function call inside the function to trigger another function which call the value.

谢谢。我将变量声明为全局变量并在内部函数中插入值,然后在函数内部进行函数调用以触发另一个调用该值的函数。

var a=0;
var surveyObjects = Parse.Object.extend(surveyObject);
var query= new Parse.Query(surveyObjects);
query.count({
    success: function(count) {a =count; total();},
    error:function(error){}
});

function total(){alert (a);}

回答by Tal87

You can do this by using implicit global variable behaviour.

您可以通过使用隐式全局变量行为来做到这一点。

function one(){
   function two(){
      a=10;
   }
  
  two();
}

one();
console.log(a);

If you don't declare a variable in javascript I.E not using the varkeyword it becomes a global variable.

如果您没有在不使用var关键字的javascript IE 中声明变量,它将成为全局变量。

for further reading:

进一步阅读:

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/var#Implicit_globals_and_outer_function_scope

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/var#Implicit_globals_and_outer_function_scope

回答by Soviut

Because of how javascript, and most languages, scope variables, you can't access variables declared inside a function from outside a function. The variable belongs to the function's scope only, not the global scope.

由于 javascript 和大多数语言的作用域变量,您无法从函数外部访问在函数内部声明的变量。变量只属于函数的作用域,不属于全局作用域。

Fortunately, functions inherit the scope of their caller. So the easiest way to make your variable accessible from outside the function is to first declare outside the function, then use it inside the function.

幸运的是,函数继承了它们调用者的作用域。因此,使您的变量可从函数外部访问的最简单方法是首先在函数外部声明,然后在函数内部使用它。

function one(){
   var a;

   function two(){
       a = 10;
       return a;
   }

   return a;
}

Note that you should be very careful about how you scope your variables. The whole point of functions is to encapsulate and isolate functionality.

请注意,您应该非常小心地确定变量的范围。函数的全部意义在于封装和隔离功能。

In the case of promises, you can declare a variable outside the promise and then set its value on success.

在 promise 的情况下,您可以在 promise 之外声明一个变量,然后在成功时设置其值。

var a;

Parse.doSomething().then(function(data) {
    a = data;
});

EDIT:Based on what you showed in the comments, you're having async issues. Promises are asynchronous meaning they don't run in sequence in your code. That's why the successand errorcallbacks exist, to be called once the promise resolves. Your alert(a)is outside the promise callback, so it runs immediately, without waiting for the Parse promise to resolve so ais still undefined. If you put the alert(a)inside the promise callback, awill have been set by that point.

编辑:根据您在评论中显示的内容,您遇到了异步问题。Promise 是异步的,这意味着它们不会在您的代码中按顺序运行。这就是successerror回调存在的原因,一旦承诺解决就被调用。您alert(a)在承诺回调之外,因此它立即运行,而无需等待解析承诺解决,因此a仍未定义。如果你把alert(a)里面的promise回调,a那点就已经设置好了。

var a;
query.count({
    success: function(count) {
        a = count;
        alert(a);
        return a;
    },
    error: function(err) {}
});

回答by suraj

//  You can simply do it by 
function test()
{
    this.name='xyz';
}
var obj = new test();

console.log(obj.name);

回答by toto

like it:

喜欢它:

function one() {
    this.two = function () {
        var a = 10;
        return a;
    }
}

var o = new one();
alert(o.two());

回答by Balakumar

use return statement to access variables globally.

使用 return 语句全局访问变量。

function(){
var k=1;//local
return k;//global
}
result=k+10;//sample