By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,382 Members | 1,645 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,382 IT Pros & Developers. It's quick & easy.

Dot notation V Bracket notation

P: n/a
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
:)
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a


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/

Jul 20 '05 #2

P: n/a
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
:)
Jul 20 '05 #3

P: n/a
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
:)
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.