我想通过firebase API拉一个值,并使其成为一个全局变量,以便我可以参考它在代码中的其他位置如何在JavaScript中使用Firebase将本地变量设置为全局变量?
我想让下面的代码工作,从火力点,并且可以使用它以外(http://jsfiddle.net/chrisguzman/jb4qLxtb/)
var ref = new Firebase('https://helloworldtest.firebaseIO.com/');
ref.on('value', function (snapshot) {
var Variable = snapshot.child("text").val();
return Variable;
});
alert(Variable);
我试着使用var没有运气定义它下面的功能(http://jsfiddle.net/chrisguzman/jb4qLxtb/1/)
var ref = new Firebase('https://helloworldtest.firebaseIO.com/');
var MyVariable = ref.on('value', function (snapshot) {
var Variable = snapshot.child("text").val();
return Variable;
});
alert(MyVariable);
我也试着将它定义作为一项功能没有运气:http://jsfiddle.net/chrisguzman/jb4qLxtb/2/
var ref = new Firebase('https://helloworldtest.firebaseIO.com/');
function myFunction() { ref.on('value', function (snapshot) {
var Variable = snapshot.child("text").val();
return Variable;
});};
alert(myFunction());
你*不*在这种情况下。请参阅http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call和http://stackoverflow.com/questions/500431/what-is-the-scope -of-variables-in-javascript – user2864740 2014-10-30 01:36:42