469,347 Members | 1,874 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,347 developers. It's quick & easy.

Any instance where bracket notation might fail?

I was perusing some code and saw:

var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}

Is this a waste of code? Or is there some instance where bracket notation might fail?

-Lost
Apr 20 '07 #1
11 2217
On 20 Apr, 15:29, "-Lost" <missed-s...@comcast.netwrote:
I was perusing some code and saw:

var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;

}

Is this a waste of code? Or is there some instance where bracket notation might fail?

-Lost
It might fail in any circumstance where the form HTML itself is
dynamically generated, depending on timing.

Apr 20 '07 #2
"Sean Inglis" <in*****@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
On 20 Apr, 15:29, "-Lost" <missed-s...@comcast.netwrote:
>I was perusing some code and saw:

var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;

}

Is this a waste of code? Or is there some instance where bracket notation might fail?
>
It might fail in any circumstance where the form HTML itself is
dynamically generated, depending on timing.
Hrmm... if that were the case then the dot notation version would fail as well.

-Lost
Apr 20 '07 #3
Lee
-Lost said:
>
"Sean Inglis" <in*****@gmail.comwrote in message
news:11**********************@y80g2000hsf.googleg roups.com...
>On 20 Apr, 15:29, "-Lost" <missed-s...@comcast.netwrote:
>>I was perusing some code and saw:

var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;

}

Is this a waste of code? Or is there some instance where bracket notation might
fail?
>>
It might fail in any circumstance where the form HTML itself is
dynamically generated, depending on timing.

Hrmm... if that were the case then the dot notation version would fail as well.
Bracket notation is more reliable than dot notation, because dot
notation would fail if you also had a global variable or function
named "aspnetForm" (unlikely in this case, of course).
--

Apr 20 '07 #4
-Lost said the following on 4/20/2007 10:29 AM:
I was perusing some code and saw:

var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}

Is this a waste of code?
Yes.
Or is there some instance where bracket notation might fail?
If bracket notation won't work then dot notation sure isn't going to
work either.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 20 '07 #5
"Randy Webb" <Hi************@aol.comwrote in message
news:ue********************@giganews.com...
-Lost said the following on 4/20/2007 10:29 AM:
>I was perusing some code and saw:

var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}

Is this a waste of code?

Yes.
Thought so.
>Or is there some instance where bracket notation might fail?

If bracket notation won't work then dot notation sure isn't going to work either.
Thought so.

Thanks!

-Lost
Apr 20 '07 #6
-Lost wrote:
>I was perusing some code and saw:

var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}

Is this a waste of code? Or is there some instance where
bracket notation might fail?
Your question betrays some misconception, either as to the nature of
property accessors or about what the code above is attempting.

Bracket notation and dot notation are the two forms of property accessor
available in javascript. Whenever the property names used in either are
literals that would qualify as Identifiers in javascript the two are
completely interchangeable. That is:-

document.forms['aspnetForm']

- may also be written as any of:-

document.forms.aspnetForm
document['forms'].aspnetForm
document['forms']['aspnetForm']
window['document'].forms.aspnetForm
window['document'].forms['aspnetForm']
window['document']['forms'].aspnetForm
window['document']['forms']['aspnetForm']

- to precisely the same effect (though with some very minor differences
in performance).

And similarly:-

document.aspnetForm

- may also be written as:-

document['aspnetForm']
window['document'].aspnetForm
window['document']['aspnetForm']

- to precisely the same effect.

The code above has no interest in any distinction between dot notation
property accessors and bracket notation property accessors, and that
distinction had no relevance for the code.

The question the code is asking is "is there a property of the -
document.forms - collection with the name 'aspnetForm'?", and if not "is
there then instead a property of the - document - with the same name?".

It almost certainly is a waste of code because if you assume that a
property with the name "aspnetForm" is intended to refer to a form
element (the alternative would be to assume the author did not perceive
the sense in giving objects non-misleading names) then there have never
been any browsers where a named form element could not be references as
a named property of the (ancient, and W3C DOM formalised) -
document.forms - collection but could then be referenced through the
common (though non-standard) HTML DOM shortcut of referring to it as a
named property of the - document - object.

Richard.

Apr 29 '07 #7
Richard Cornford wrote:
It almost certainly is a waste of code because if you assume that a
property with the name "aspnetForm" is intended to refer to a form
element (the alternative would be to assume the author did not perceive
the sense in giving objects non-misleading names) then there have never
been any browsers where a named form element could not be references as
a named property of the (ancient, and W3C DOM formalised) -
document.forms - collection but could then be referenced through the
common (though non-standard) HTML DOM shortcut of referring to it as a
named property of the - document - object.
For application/xhtml+xml documents Mozilla has it the other way,
document.forms.formName is implemented but document.formName is not:
<http://home.arcor.de/martin.honnen/javascript/test2007042901.xhtml>

So in the case of application/xhtml+xml the code

var theForm = document.aspnetForm;
if (!theForm) {
theForm = document.forms['aspnetForm'];
}

would make some sense but simply doing

var theForm = document.forms['aspnetForm'];

would suffice.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 29 '07 #8
Martin Honnen wrote:
Richard Cornford wrote:
>It almost certainly is a waste of code because if you assume
that a property with the name "aspnetForm" is intended to
refer to a form element (the alternative would be to assume
the author did not perceive the sense in giving objects
non-misleading names) then there have never been any browsers
where a named form element could not be references as a named
property of the (ancient, and W3C DOM formalised) - document.forms -
collection but could then be referenced
through the common (though non-standard) HTML DOM shortcut of
referring to it as a named property of the - document - object.

For application/xhtml+xml documents Mozilla has it the other way,
document.forms.formName is implemented but document.formName is not:
<http://home.arcor.de/martin.honnen/javascript/test2007042901.xhtml>
Which was my reason for stating "HTML DOM shortcut", to imply the
possible non-applicability/availability of those shortcuts in XHTML
DOMs.

I suppose that because most of the people who think that they are
working with XHTML are just deluding themselves and actually working
with (error-filled tag-soup) HTML, it may be worth being more explicit
about the distinction between those two types of DOM. Though it has been
said often enough in the past that the current apparently pandemic
failure to see the distinction seems wilful.
So in the case of application/xhtml+xml the code

var theForm = document.aspnetForm;
if (!theForm) {
theForm = document.forms['aspnetForm'];
}

would make some sense but simply doing

var theForm = document.forms['aspnetForm'];

would suffice.
Absolutely.

Richard.

Apr 29 '07 #9
Richard Cornford wrote:
I suppose that because most of the people who think that they are
working with XHTML are just deluding themselves and actually working
with (error-filled tag-soup) HTML, it may be worth being more explicit
about the distinction between those two types of DOM.
Any links to a more complete discussion of this?

--
C.W.Holeman II | cw***@Julian5Locals.com-5 http://JulianLocals.com/cwhii
To only a fraction of the human race does God give the privilege of
earning one's bread doing what one would have gladly pursued free, for
passion. I am very thankful. The Mythical Man-Month Epilogue/F.P.Brooks
Apr 30 '07 #10
On May 1, 6:32 am, "C.W.Holeman II" <cwhii_google_s...@yahoo.com>
wrote:
Richard Cornford wrote:
I suppose that because most of the people who think that they are
working with XHTML are just deluding themselves and actually working
with (error-filled tag-soup) HTML, it may be worth being more explicit
about the distinction between those two types of DOM.

Any links to a more complete discussion of this?
Search news:comp.infosystems.www.authoring.html, here's a recent
thread, there are plenty of others:

<URL:
http://groups.google.com.au/group/co...77eb3e525f6b91
>

--
Rob

Apr 30 '07 #11
On Apr 20, 4:33 pm, "-Lost" <missed-s...@comcast.netwrote:
"Sean Inglis" <ingl...@gmail.comwrote in message

news:11**********************@y80g2000hsf.googlegr oups.com...
On 20 Apr, 15:29, "-Lost" <missed-s...@comcast.netwrote:
I was perusing some code and saw:
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
Is this a waste of code? Or is there some instance where bracket notation might fail?
It might fail in any circumstance where the form HTML itself is
dynamically generated, depending on timing.

Hrmm... if that were the case then the dot notation version would fail as well.

-Lost
Ah yes. I'm talking balls. My bad.

May 7 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.