-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 に答える