Dot notation V Bracket notation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Mark Bram

    Dot notation V Bracket notation

    Howdy All!

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

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

    Can I access attributes and objects equally with both?

    Thanks for any advice!

    Rob
    :)


  • Martin Honnen

    #2
    Re: Dot notation V Bracket notation



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

    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.formna me
    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


    Comment

    • Robert Mark Bram

      #3
      Re: Dot notation V Bracket notation

      Howdy Martin,

      Thanks for the response!
      [color=blue]
      > 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.formna me
      > you need
      > document["formname"]
      > as the equivalent bracket notation.[/color]

      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? :)
      [color=blue]
      > 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"][/color]

      This is a good point. Thank you.

      Rob
      :)


      Comment

      • Robert Mark Bram

        #4
        Re: Dot notation V Bracket notation

        I see now...
        [color=blue]
        > 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]);[/color]

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

        Got it.

        Rob
        :)


        Comment

        Working...