javascript中的函数内部

javascript中的函数内部

本文介绍了访问函数内部的变量,该变量位于javascript中的函数内部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I access a variable inside a function which is inside a function in 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);

a 显示 undefined 值.我需要在外部使用 a 的值.

a is showing undefined value. I need to use the value of a outside.

推荐答案

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

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

这篇关于访问函数内部的变量,该变量位于javascript中的函数内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:02