Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

mootools - Javascript Object returns junk values

I'm trying to assign values to a javascript object and when doing so, some junk values end up in there which seem like array methods like 'push', 'pop','splice' etc. The following is my code.

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();
}

I am not sure what I'm missing here but it looks like I certainly am. Any help on this would be greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Never use for...in on an array. Period. The garbage values you are seeing are properties of the array prototype.

See this related question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...