Dot notation V Bracket notation

  • Thread starter Robert Mark Bram
  • Start date
R

Robert Mark Bram

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
:)
 
M

Martin Honnen

Robert said:
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"]
 
R

Robert Mark Bram

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
:)
 
R

Robert Mark Bram

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
:)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top