6. OOPS - object oriented obsessions
Let's look once more at our second script:
My second script
|
<script language='JavaScript'>
<!--
var whoami=navigator.userAgent.toLowerCase(); //use lower case name
var my_version = parseInt(navigator.appVersion); //get version
var is_ie = (whoami.indexOf("msie") != -1); //does name contain 'msie'?
var is_ie3 = (is_ie && (my_version < 4)); //is it version 3?
if ( is_ie3 )
{ alert ("Aargh! You're using Internet Explorer 3 !");
} else
{ alert ("Good! Your Browser appears JavaScript-competent!"); };
//-->
</script>
|
We are just beginning to understand what's going on. We set up four
variables - whoami, my_version, is_ie and
is_ie3. The first presumably contains a "string" that identifies
the browser being used, the second tells us the version of the browser,
the third tells us if the browser is internet explorer, and the fourth
whether this is version 3 (version number under four && internet explorer).
We then use an if statement to either harrass the unfortunate user, or
compliment her.
Armed with this knowledge, let's look at the mysterious line:
var whoami=navigator.userAgent.toLowerCase(); //use lower case name
in a bit more detail. Notice how we have a strange sequence of words linked
together by dots, thus: navigator.userAgent.toLowerCase
This is typical of Object-Orientated Programming (OOP). The idea behind
OOP is that we have things called "objects". These objects have two
distinct attributes associated with them. They have:
- properties
- methods
The commonly-used analogy is to a motor car - it has properties:
the paint is red, it has five wheels (including the spare), and so on;
it also has methods - start the car,
take off the handbrake and so on. (Can you tell the difference
between a method and a function? I'm not sure I can)!
Trivially, one would suppose that you could say things like:
car.paint="red";
car.start();
and so on.
Let's put this in context.
- navigator.userAgent refers to
an object called navigator that has a property called
userAgent.
- navigator.userAgent.toLowerCase() takes
that property, applies the method toLowerCase to the property,
and gives it to us.
Clearly all this does is give us a string that
contains the name of the internet browser we are dealing with, after
this string has been converted to lower case.
If you were brought up on more traditional computer programming
languages, you'll see that OOP doesn't do anything that cannot be
done in traditional computer languages. It's just a different way
of looking at information and how we handle it. (Later on we will
explore the merits and demerits of OOP). With JavaScript, we're
stuck with OOP, so it's a good idea to get to know it.
Let's now look at all of our OOP-style statements:
- whoami=navigator.userAgent.toLowerCase();
- my_version = parseInt(navigator.appVersion);
- is_ie = (whoami.indexOf("msie") != -1);
We can deduce a lot from these statements. The first tells us that
all browsers that understand JavaScript have an object called navigator
and that this has a property called userAgent. The second tells
us that navigator has another property, appVersion.
The third statement is even more interesting, because it tells us
that something we have created will automatically (automagically?)
acquire certain methods that go with it. For we created the
variable whoami but look what we can do with it - we can
apply the mysterious method indexOf to it, and ultimately get an intelligible
answer - the value true or false that is put into is_ie, as we
discussed above!
This last observation hints at an important characteristic of OOP -
objects can "inherit" properties and methods. This is both very useful (you don't
have to potter around associating some commonly-used methods with an
object), and also a cause of immense frustration. Why frustration? Because
in any fairly complex object-oriented system (for example a web browser)
there is a massive proliferation of properties and methods, that can
confuse and mystify you if you want to sit down and tell the browser to
do something! Later on we will try and detail every property and method
of Netscape and MSIE, and you will see what I mean.
For now, let's just look at examples of the three methods we've been introduced to:
- toLowerCase()
- indexOf("string")
- parseInt("123abc")
With each of the above examples, we've included examples of 'arguments' that
the methods expect (within parenthesis). For example, indexOf
expects to be provided with a string, and parseInt should be
supplied with a string that starts with a number. toLowerCase doesn't expect anything,
so we've given it nothing but empty parenthesis.
It is obvious what toLowerCase does - it converts something
to lower case. For example, "FrED" might become "fred". indexOf is
a bit more puzzling, but let's look at how it's used (with the comment):
var is_ie = (whoami.indexOf("msie") != -1); //does name contain 'msie'?
Using our previously-acquired knowledge, we can work out the meaning
of this line:
we can now deduce what indexOf does - it gives us a number, and this
number presumably tells us where within a string, a particular sequence
of characters is located. If the number is "-1", then presumably the
desired sequence was not found.
Let's try an example. Say we have the sentence
"This is not a pipe". We'll try the following thought experiment:
var fred="This is not a pipe"; // create fred and allocate a value
var findis=fred.indexOf("is"); // find where the string "is" is located
Now quick - what should the value of findis be? (No, don't cheat and
page down before you've worked out the answer)!
Full marks if you said "2". My suspicion is that you said either six,
five, or perhaps three. Let's look at our experiment again:
var fred="This is not a pipe"; // create fred and allocate a value
var findis=fred.indexOf("is"); // find where the string "is" is located
Counting from the left, we see that the first character is T, the
second is h, the third is i and the fourth is s. So the third and fourth
characters of the string make up the string "is", which matches. So
why is the answer two and not three? Simply because grown up computer
languages count starting at zero!! So the 'zeroth' character is actually
T, the 1st is h, the 2nd is i and the 3rd is s. (I bet at least some
readers missed the "is" in "This" and jumped forward to the word "is"
later on in the sentence).
Remember this method of counting when you work with computers. Start
with zero!
7. Putting it together
We now know enough to create a precise English translation of our
previously obscure second script:
My second script (complete!)
|
<script language='JavaScript'>
<!--
var whoami=navigator.userAgent.toLowerCase(); //use lower case name
var my_version = parseInt(navigator.appVersion); //get version
var is_ie = (whoami.indexOf("msie") != -1); //does name contain 'msie'?
var is_ie3 = (is_ie && (my_version < 4)); //is it version 3?
if ( is_ie3 )
{ alert ("Aargh! You're using Internet Explorer 3 !");
} else
{ alert ("Good! Your Browser appears JavaScript-competent!"); };
//-->
</script>
|
.. translates as
- Create a string variable whoami and set it to contain
a string that describes the userAgent of the navigator
object that is browsing this page;
- Create another string called my_version that gives us
the version of the navigator (By the way, we haven't yet said what
parseInt does, but it's clear from the context that it
takes a string and pulls out the first integer it comes across -
a rather clumsy function that shows how some languages are simply
thrown together!);
- Find out whether the string describing the userAgent contains
the sub-string "msie". If it does, set the newly created variable
is_ie to true, otherwise set is_ie to false;
- If the version number is under four, and is_ie is true,
then set the new variable is_ie3 to true, but otherwise
make it false;
- If we are dealing with Internet Explorer 3, in other words,
if is_ie3 is true, then print the rude and damning message
"Aargh! You're using Internet Explorer 3 !" in a cute little box,
and otherwise:
- Congratulate the user for good taste with the message:
"Good! Your Browser appears JavaScript-competent!"
A piece of cake!
Easy, isn't it? Well, if you've got this far, then the rest of
JavaScript should actually be a piece of cake. For, surprisingly
enough, we've covered so many vital concepts in the above, that
almost everything will now slot into place. Before we rush on to our
next lesson, I need some information from you (I don't mind if you lie, Ms Wstfgl!)