问题
Possible Duplicate:
Return Global Variable from Javascript Method
I have this.
var thisData = "";
function calculateThings(newData) {
thisData = newData.things.otherthings //has a value of 10;
}
alert(thisData) //returns nothing
What am I doing wrong?
回答1:
you need to call your function:
calculateThings(newData);
should be more like:
var thisData = "";
function calculateThings(data) {
thisData = data.things.otherthings //has a value of 10;
}
calculateThings(newData);
alert(thisData) //returns nothing
where data is your parameter, and you can pass whatever you want into it.
回答2:
You created a function but never call it. You need to call it via:
var thisData = "";
function calculateThings(newData) {
thisData = newData.things.otherthings //has a value of 10;
}
alert(calculateThings(thisData));
or you can self-invoke the function like:
(function calculateThings(newData) {
thisData = newData.things.otherthings //has a value of 10;
})()
来源:https://stackoverflow.com/questions/12427059/variable-not-being-set