Martin

Navigation

Skip navigation.

Search

Site navigation

Email conversation

Taken from the discussion of a blog post.

FromMartin
ToMe
SubjectFirebug and javascript.options.strict
Date30 August 2007 13:09
You still can check for properties without causing a warning.

if( document.onclick == myFunction ) {

from [the] examples becomes

if( typeof document.onclick !== "undefined" &&
  document.onclick === myFunction ) {

The typeof operator does not issue a warning about accessing a undefined
variable / property.
FromMe
ToMartin
SubjectRe: Firebug and javascript.options.strict
Date08 Jan 2008 11:47
> if( document.onclick == myFunction ) {
> from his examples becomes
> if( typeof document.onclick !== "undefined" &&
>   document.onclick === myFunction ) {

This approach is simply wasteful and pointless. The former version is
perfectly valid because JavaScript is loosely typed and can compare any
variable type against any other variable type. Your code performs more than
twice as slowly - a more than 100% performance hit, just because you think
that a warning is an error.

It's a warning not an error, meaning you can ignore it (that's what my
article was about, by the way, authors like you who mistakenly treat
warnings as errors and think they all should be removed, even when the code
is already perfectly valid). Stop trusting that stupid setting, ignore the
warning.

Suggesting that people should use a typeof comparison instead of
if(document.onclick) is equally silly, and will not work anyway. Some
browsers use null as the default value, while others use undefined. typeof
null is object, and that will never equate to undefined. My code will always
give the correct response (and that part of your code will also correct the
error of your typeof check). So basically your code is even more pointless,
and exists _only_ for the purpose of satisfying an overly paranoid debugger.
What a waste of effort for real browsers. Stop trusting that stupid setting.
Learn to ignore it.

Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.