Variable not being set [duplicate]

天大地大妈咪最大 提交于 2020-01-16 08:08:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!