Doug Wright

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromDoug Wright
ToMe
SubjectMozilla JS warnings
Date9 May 2006 22:33
Hi

http://www.howtocreate.co.uk/strictJSFirefox.html

I quote

> Take this well written script for example:
> if( document.body.childNodes[10] == e.target ) {
>
> If there is no childNode, Firefox's strict syntax check will throw a
> warning, saying "Warning: reference to undefined property
> document.body.childNodes[10]". But I know it might not be defined, that is
> why I checked for it! And Firefox tells me off for checking for it.

Now I'm certainly not a JavaScript 'expert', but it seems to me that the
warning is right, because you aren't checking if
document.body.childNodes[10] exists, before checking it's value.

The correct way is surely
if(document.body.childNodes[10] && document.body.childNodes[10]==e.target){

Also the statement

> In JavaScript that is not really necessary since it will automatically
> create a variable in the current scope anyway

is wrong - undeclared variables are always global scope.

Have you filed a Mozilla bug for the spurious 'while(n=getNext())' warning?


Doug
FromMe
ToDoug Wright
SubjectRe: Mozilla JS warnings
Date9 May 2006 22:51
Doug,

> Now I'm certainly not a JavaScript 'expert', but it seems to me that the
> warning is right, because you aren't checking if
> document.body.childNodes[10] exists, before checking it's value.

Yes, and that is perfectly valid, so the warning is just confusing and
wrong. I am checking if it equals the desired value. Since it is undefined,
it is not the desired value, so my test is valid, and correct, and certainly
does not deserve a warning.

> The correct way is surely
>> if(document.body.childNodes[10] && document.body.childNodes[10]==e.target){

No. It is not needed, it is just wasting CPU cycles.

> Also the statement
>> In JavaScript that is not really necessary since it will automatically
>> create a variable in the current scope anyway
> is wrong - undeclared variables are always global scope.

Fixed, thanks.

> Have you filed a Mozilla bug for the spurious '|while(n=getNext())'
> warning?

No, because it is not an error, just a warning. The warning is stupid, but
it is not wrong, just confusing. So it is not really a bug, just not
helpful.


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromDoug Wright
ToMe
SubjectRe: Mozilla JS warnings
Date9 May 2006 23:18
I've got no intention of having a long argument via email over this
(especially as I'm not 100% sure of my ground), but I still believe the
warning is correct.

A (defined) property without a value (undefined) != a non-existent
(undefined) property.
FromMe
ToDoug Wright
SubjectRe: Mozilla JS warnings
Date9 May 2006 23:27
Doug,

> I've got no intention of having a long argument via email over this
> (especially as I'm not 100% sure of my ground), but I still believe the
> warning is correct.

Then simply trust me. there is absolutely nothing at all wrong with what I
did, and it can be used to make your scripting more efficient, and mking it
easy to write cross browser scripts. Checking an undefined value against a
real value is perfectly ok. This is ECMAScript 3, which does not have script
datatyping. If this were C or C++, then fine, but it isn't. The warning may
help the odd developer who _really_ knows what they are doing, but far more
often, it confuses the less experienced coder into thinking they did
something wrong, when they didn't. You yourself (through no fault of your
own) seem to have fallen into that trap, confirming what I am saying in that
article.
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.