Dot notation V Bracket notation

Discussion in 'Javascript' started by Robert Mark Bram, Jul 4, 2003.

  1. Howdy All!

    Is there any difference between referencing objects and attributes with dot
    notation or bracket notation?

    Example,
    document.formname.controlname
    document [formname] [controlname]

    Can I access attributes and objects equally with both?

    Thanks for any advice!

    Rob
    :)
     
    Robert Mark Bram, Jul 4, 2003
    #1
    1. Advertising

  2. Robert Mark Bram wrote:
    > Howdy All!
    >
    > Is there any difference between referencing objects and attributes with dot
    > notation or bracket notation?
    >
    > Example,
    > document.formname.controlname
    > document [formname] [controlname]
    >
    > Can I access attributes and objects equally with both?
    >
    > Thanks for any advice!


    Member access is defined with the bracket notation but note that inside
    the brackets you need an expression which is then evaluated and
    converted to a string. Thus above for
    document.formname
    you need
    document["formname"]
    as the equivalent bracket notation.
    The bracket notation is more flexible as you can put any expression like
    a variable in there, the dot notation is shorter but you need to know
    member names at coding time and you can only use valid identifiers while
    the bracket notation allows arbritrary names e.g.
    document["form-name"]




    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jul 4, 2003
    #2
    1. Advertising

  3. Howdy Martin,

    Thanks for the response!

    > Member access is defined with the bracket notation but note that inside
    > the brackets you need an expression which is then evaluated and
    > converted to a string. Thus above for
    > document.formname
    > you need
    > document["formname"]
    > as the equivalent bracket notation.


    But what about attribute access? Let's say I have a form called "navBar"
    with a select called "sites". I can do this:
    alert (document [navBar][sites].value);
    but not this:
    alert (document [navBar][sites][value]);

    So bracket notation cannot access attributes right? Or am I missing
    something? :)

    > The bracket notation is more flexible as you can put any expression like
    > a variable in there, the dot notation is shorter but you need to know
    > member names at coding time and you can only use valid identifiers while
    > the bracket notation allows arbritrary names e.g.
    > document["form-name"]


    This is a good point. Thank you.

    Rob
    :)
     
    Robert Mark Bram, Jul 5, 2003
    #3
  4. I see now...

    > But what about attribute access? Let's say I have a form called "navBar"
    > with a select called "sites". I can do this:
    > alert (document [navBar][sites].value);
    > but not this:
    > alert (document [navBar][sites][value]);


    alert (document [navBar][sites]["value"]);

    Got it.

    Rob
    :)
     
    Robert Mark Bram, Jul 5, 2003
    #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. Samuël van Laere

    To dot or not to dot?

    Samuël van Laere, Oct 16, 2003, in forum: HTML
    Replies:
    8
    Views:
    429
    Samuël van Laere
    Oct 16, 2003
  2. Asen Bozhilov

    Square bracket and dot notations?

    Asen Bozhilov, Jun 11, 2011, in forum: Python
    Replies:
    4
    Views:
    1,032
    Terry Reedy
    Jun 11, 2011
  3. larry

    JavaScript bracket notation

    larry, Aug 8, 2005, in forum: Javascript
    Replies:
    3
    Views:
    119
    Matt Kruse
    Aug 9, 2005
  4. Replies:
    6
    Views:
    252
    Thomas 'PointedEars' Lahn
    Dec 12, 2005
  5. -Lost
    Replies:
    9
    Views:
    152
    seani
    May 7, 2007
Loading...

Share This Page