我有两个模块,并且在第一个模块中声明了一个对象,因为我知道基元是通过java脚本中的值传递的和对象的引用。我想从请求中获取响应状态,并将对象作为参考传递,以便我可以修改它的属性。问题在于它没有做任何事情。最后,值会是一样的。在java脚本中更改函数内部某个对象的属性值不起作用,因为它应该是
//this code is in a different module from the other one
var variableToBeChanged = { something : "Initial value" };
anotherModule.changeValue(variableToBeChanged);
alert(variableToBeChanged.something);
//and in the other module I have a $.ajax and I want to get the response status(ex. 200)
//the code is sth like this:
function AnotherModule(ajax){
function changeValue(variableToBeChanged){
...
...
...
$.ajax({
...
...
...
success: function(data,xhr){
variableTobechanged.something = xhr.status;
}
});
}
}
最终它将显示: “初始值”,而不是200或其他任何东西。 我在这里做错了什么?
的变量被异步改变,所以你必须使用的承诺或回调进入新的价值 –