I have an object and I would like to loop through it and print out all the values of its properties. My problem is that when I try to print out the value returned by one of its methods, I get the code of the method instead of the value the method should return. I'm sure I'm making an access syntax typo, but I can't figure it out.
function Dog (breed,sound) {
this.breed = breed;
this.sound = sound;
this.bark = function(){
alert(this.sound);
return this.sound;
};
}
var x = new Dog("Retriever",'woof');
x.bark(); // Test: 'woof'
for (var y in x) {
document.getElementById("results").innerHTML +="<br/>"+x[y];
}
/* x[y] when y is 'bark' returns the method's code,
but I'm looking for the value. */
JSFiddle: http://jsfiddle.net/nysteve/QHumL/4/