0

I seem to have a problem with objects' properties' scope. I would like to output each of the Message objects' title and message properties to a select element, but it is Not Working! What am I doing incorrectly

<html><head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    function Message(title, message) {
        this.title=title;
        this.message=message;
        this.getTitle = function(){
            return this.title;
        };
        this.getMessage = function(){
            return this.message;
        };
    }
    var messages = new Array(
        new Message("First Title", "This is the first message"),
        new Message("Second Title", "This is another message")
    );
    function updateSelect () {
        $("#cannedMessages_button").empty();
        for (c in messages) {
            // First try, with getters and setters
            $("#cannedMessages_button").append($('<option>', { value : c.getMessage() , text : c.getTitle() }));
            // Second try, directly
            $("#cannedMessages_button").append($('<option>', { value : c.message , text : c.title }));
        }
    }
    updateSelect();
});
</script>
</head><body>
<form><select id="cannedMessages_button"></select></form>
</body></html>

I can verify that the foreach is in fact running two iterations, but I cannot get the values out of the objects.

dotancohen
  • 30,064
  • 36
  • 138
  • 197
  • 5
    You are iterating with `for...in` over an array which you should not do, but that is not the problem. `c` is not the element of the array, but the current property name (index). See the [MDN documentation](https://developer.mozilla.org/en/JavaScript/Reference/Statements/for...in) – Felix Kling Jan 30 '12 at 14:30
  • 1
    You should look into [looping in jQuery](http://api.jquery.com/jQuery.each/) – ori Jan 30 '12 at 14:34

2 Answers2

1

don't use for (c in messages).

in is for iterating over properties of an object, not for iterating over values in an array.

Use the tried and true

for(var i = 0; i < messages.length; i++) {
...
}

Also, you are not putting your getTitle and getMessage methods on a prototype, which is kind of wasteful.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • Thank you! What is the advantage to putting the methods on a prototype? The syntax (being outside of the object block) looks like it will confuse me later (coming from a C family background, in which class methods are declared inside the class code block). – dotancohen Jan 30 '12 at 14:58
  • 1
    dotancohen that question is very popular on SO. See http://stackoverflow.com/questions/4650513/why-is-javascript-prototyping/4650576#4650576 and also http://stackoverflow.com/questions/8874115/prototypal-inheritance-best-practices/8874140#8874140 – hvgotcodes Jan 30 '12 at 15:02
  • Thank you. I see than I am in Rome, I had better get used to doing as the Romans do (paraphrasing a commenter on one of those links). – dotancohen Jan 30 '12 at 15:18
0

the syntax for the for in loop in js is :

for(var key in obj)
    {
        var currentElement = obj[key];
    }
gion_13
  • 41,171
  • 10
  • 96
  • 108