Any instance where bracket notation might fail?

L

-Lost

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
 
S

Sean Inglis

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.
 
L

-Lost

Sean Inglis said:
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
 
L

Lee

-Lost said:
Sean Inglis said:
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).


--
 
L

-Lost

Randy Webb said:
-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.
If bracket notation won't work then dot notation sure isn't going to work either.

Thought so.

Thanks!

-Lost
 
R

Richard Cornford

-Lost said:
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.
 
M

Martin Honnen

Richard said:
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.
 
R

Richard Cornford

Martin said:
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.
 
S

seani

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.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top