Frustrating...

Discussion in 'Javascript' started by Trevor, Feb 12, 2006.

  1. Trevor

    Trevor Guest

    Why does alert(data[3]); work fine while var subject = data[3];
    give me the error, "Error: data has no properties"?

    for (i = 0; i < (data.length); i++) {
    alert(data[3]); //this works fine!
    var subject = data[3]; //this gives error!
    var sender = data[4];
    var date = data[5];
    }

    Thanks for your help.

    Trevor
     
    Trevor, Feb 12, 2006
    #1
    1. Advertising

  2. alert() works, variable definition does not (was: Frustrating...)

    Trevor wrote:

    > Why does alert(data[3]); work fine while var subject = data[3];
    > give me the error, "Error: data has no properties"?
    >
    > for (i = 0; i < (data.length); i++) {


    The inner parantheses are redundant, and

    for (var i = 0, len = data.length; i < len; i++)
    {

    is safer and more efficient. And since order does not seem to matter,
    the following is even more efficient:

    for (var i = data.length; i--;)
    {

    > alert(data[3]); //this works fine!
    > var subject = data[3]; //this gives error!
    > var sender = data[4];
    > var date = data[5];
    > }


    That depends on your data. Literally.

    However, I rather think you did not locate the actual error properly.


    PointedEars

    P.S.: Do not use the tab character to indent (posted) source code.

    P.P.S.: Do not misuse the Subject header for expressing your feelings;
    Use it to describe the subject of your posting, because the
    Subject header is the first thing anyone sees of your posting,
    and it influences his/her decision to read or not to read the
    posting.
     
    Thomas 'PointedEars' Lahn, Feb 12, 2006
    #2
    1. Advertising

  3. Re: alert() works, variable definition does not

    Ian Collins wrote:

    >> The inner parantheses are redundant, and
    >>
    >> for (var i = 0, len = data.length; i < len; i++)
    >> {
    >>
    >> is safer and more efficient. And since order does not seem to matter,
    >> the following is even more efficient:
    >>
    >> for (var i = data.length; i--;)
    >> {
    >>

    > Just out of curiosity, is there any performance benefit in using --i?


    Generally, some say so. Specifically, there is none here as you want
    evaluation of `i' as continue-condition _before_ its value is decreased.
    Using `--i' here instead would force you to compare `--i > -1' and the
    like to achieve the same, while you do not need to with `i--' as `0'
    evaluates to `false' in (this) boolean expression(s) already.

    Please provide attribution of quoted material; your Mozilla includes
    that line automagically for a reason.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Feb 13, 2006
    #3
  4. Re: alert() works, variable definition does not

    Thomas 'PointedEars' Lahn wrote:

    > Trevor wrote:


    [snip]

    >> for (i = 0; i < (data.length); i++) {

    >
    > The inner parantheses are redundant, and
    >
    > for (var i = 0, len = data.length; i < len; i++)
    > {
    >
    > is safer and more efficient.


    Why is it *safer* ?

    --
    Riccardo
     
    Riccardo from Castle, Feb 13, 2006
    #4
    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. dan m

    painfully ignorant but frustrating

    dan m, Feb 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    319
    Alvin Bruney [MVP]
    Mar 2, 2004
  2. Simon Harvey
    Replies:
    1
    Views:
    330
    Guest
    Apr 2, 2004
  3. michael
    Replies:
    0
    Views:
    444
    michael
    May 1, 2004
  4. rtmc
    Replies:
    7
    Views:
    413
  5. Roy
    Replies:
    2
    Views:
    3,142
Loading...

Share This Page