Java Reference
In-Depth Information
If you call this function, you get an error message similar to the one shown in Figure 4-2.
Figure 4-2
The error here is actually a simple typo in the function defi nition. The fi rst parameter has the typo: it
should read
parameterOne
, not
parametrOne
. What can be confusing with this type of error is that
although the browser tells us the error is on one line, the source of the error is on another line.
Case Sensitivity
This is a major source of errors, particularly because it can be diffi cult to spot at times.
For example, spot the three case errors in the following code:
var myName = “Jeremy”;
If (myName == “jeremy”)
alert(myName.toUppercase());
The fi rst error is the
if
keyword; the code above has
If
rather than
if
. However, JavaScript won't tell
us that the error is an incorrect use of case, but instead IE will tell us
Object
expected
and Firefox will
tell us that
If
is
not
defined
. Although error messages give us some idea of what's gone wrong, they
often do so in an oblique way. In this case IE thinks you are trying to use an object called an
If
object
and Firefox thinks you are trying to use an undefi ned function called
If
.
Okay, with that error cleared up, you come to the next error, not one of JavaScript syntax, but a logic
error. Remember that
Jeremy
does not equal
jeremy
in JavaScript, so
myName
==
“jeremy”
is
false
,
even though it's quite likely that you didn't care whether the word is
jeremy
or
jeremy
. This type of
error will result in no error message at all, just the code not executing as you'd planned.
The third fault is with the
toUpperCase()
method of the
String
object contained in
myName
. The
previous code uses
toUppercase
, with the
C
in lowercase. IE will give us the message
Object
doesn't
support
this
property
or
method
and Firefox will report that
myName.toUppercase
is
not
a
function
. On fi rst glance it would be easy to miss such a small mistake and start checking your JavaScript
reference guide for that method. You might wonder why it's there, but your code is not working. Again,
you always need to be aware of case, something that even experts get wrong from time to time.