I need help with a JavaScript issue. I have an object that I want to iterate through in order to display all the values of its properties. However, when I try to print out the value returned by one of its methods, I am only getting the code of the method instead of the actual value it should return. I believe I may be making a mistake in my syntax for accessing the method, but I can't seem to find where I'm going wrong.
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(); // Expected output: 'woof'
for (var y in x) {
document.getElementById("results").innerHTML += "<br/>" + x[y];
}
/* When y is 'bark', x[y] returns the method's code,
whereas I am trying to get the actual value. */
JSFiddle: http://jsfiddle.net/nysteve/QHumL/4/