我正在尝试为 javascript 对象赋值,在这样做时,一些垃圾值最终出现在其中,看起来像数组方法,如“push”、“pop”、“splice”等。以下是我的代码.
function myTest(){
var userArray = new Object();
var req = new Request.JSON({
url: '/myTest.php',
method: 'post',
noCache: true,
data: 'userID=999',
onSuccess: function(json){
for(var key in json){
userArray = json[key];
for (var row in userArray){
alert(row) // This returns values like '$family','push','pop', 'reverse' etc.
}
}
},
onException: function(xhr){
alert("Unable to process your request");
},
onFailure: function(xhr){
alert("Unable to connect to the server");
}
}).send();
}
我不确定我在这里遗漏了什么,但看起来我确实遗漏了什么。对此的任何帮助将不胜感激。
最佳答案
永远不要在数组上使用 for...in。时期。您看到的垃圾值是数组原型(prototype)的属性。
查看此 related question .
关于Javascript 对象返回垃圾值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12285112/