For loops...

Discussion in 'Javascript' started by Gregc., Aug 30, 2006.

  1. Gregc.

    Gregc. Guest

    Hi
    I am having trouble understanding for loops. If I have a loop that
    says:
    var coffee = new Array();
    coffee ["mixedblend"] = 5.50;
    for (c in coffee)
    {code goes here}


    That for saying while there is a c in coffee, then conduct the code.
    Is that a correct interpretation?


    Greg
     
    Gregc., Aug 30, 2006
    #1
    1. Advertising

  2. Gregc. wrote:
    > Hi
    > I am having trouble understanding for loops. If I have a loop that
    > says:
    > var coffee = new Array();
    > coffee ["mixedblend"] = 5.50;
    > for (c in coffee)
    > {code goes here}
    >
    >
    > That for saying while there is a c in coffee, then conduct the code.
    > Is that a correct interpretation?


    It is good that you asked. First, for this application you should use an Object,
    not an Array.

    var coffee = {};

    It is usually better to use the dot notation. Save the subscript notation for
    the cases where dot notation isn't allowed.

    coffee.mixedblend = 5.50;

    Make sure that the loop variable has been declared locally. You can declare it
    in the for statement if you want to.

    for (var c in coffee) {

    Within the loop, c is the current name, and coffee[c] is the current value. It
    is usually a good idea to filter out unwanted stuff that might be in the
    inheritance chain.

    if (coffee.hasOwnProperty(c)) {

    code goes here

    }
    }

    http://javaascript.crockford.com/
     
    Douglas Crockford, Aug 30, 2006
    #2
    1. Advertising

  3. Gregc.

    RobG Guest

    Gregc. wrote:
    > Hi
    > I am having trouble understanding for loops. If I have a loop that
    > says:
    > var coffee = new Array();
    > coffee ["mixedblend"] = 5.50;


    That uses an Array as a plain object, you are not using any of its
    special array properties. You could have written:

    var coffee = new Object();
    coffee ["mixedblend"] = 5.50;

    Though the use of an initialiser or object literal is generally
    preferred:

    var coffee = { mixedblend : 5.50 };


    > for (c in coffee)
    > {code goes here}


    for (c in coffee ){
    alert( coffee[c] );
    }


    > That for saying while there is a c in coffee, then conduct the code.
    > Is that a correct interpretation?


    Almost - change "while there is a c" to "for each enumerable property".

    It is usually called a for..in loop to differentiate it from a
    conditional for loop. A for..in loop goes over all the enumerable
    properties of the object in no particular order (some implementations
    will go over them in the order they were added, it is not reliable).
    Most built-in objects like the JavaScript Array object have many
    non-enumerable properties (e.g. you can't use for..in to get the length
    property of an Array).

    It is impossible for you to add non-enumerable properties, or to make
    the non-enumerable ones enumerable (though you can mask them to give
    the appearance of enumerability).


    --
    Rob
     
    RobG, Aug 30, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. eismaus4

    to many FOR loops?

    eismaus4, Apr 27, 2004, in forum: VHDL
    Replies:
    1
    Views:
    699
  2. Jeremy Morton

    Perl loops should use break, not last

    Jeremy Morton, Jan 29, 2005, in forum: Perl
    Replies:
    1
    Views:
    5,172
    Big and Blue
    Jan 30, 2005
  3. Multiple For Loops?

    , Feb 22, 2006, in forum: VHDL
    Replies:
    1
    Views:
    487
    john Doef
    Feb 22, 2006
  4. =?Utf-8?B?Um9iIE8nQ29w?=

    RedirectFromLoginPage and loops

    =?Utf-8?B?Um9iIE8nQ29w?=, Oct 25, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    421
    =?Utf-8?B?Um9iIE8nQ29w?=
    Oct 25, 2004
  5. Me
    Replies:
    2
    Views:
    260
Loading...

Share This Page