-3

我有一个方法:

function calculateThings(newdata){


}

如何从其中返回全局变量?

谢谢!

抱歉没有详细说明。

var thisData = "";

function calculateThings(newData) {

    thisData = newData.things.otherthings //has a value of 10;

}

alert(thisData) //returns nothing

我究竟做错了什么?

4

1 回答 1

2

There doesn't seem to be much point in returning a global variable, the function can just set it and other functions reference it.

var setGlobal = (function(global) {
  return function(value) {
    global.someVarName = value;
  }
}(this));

var readGlobal = (function(global) {
  return function() {
    return global.someVarName;
  }
}(this));

setGlobal('foo');
alert(readGlobal()); // foo
alert(someVarName);  // foo
于 2012-09-14T14:10:16.430 回答