JavaScript
Notes:
I
have kept these notes as I go through JavaScript and decided to post them
because it may answer some questions.
- There is a client-side
JavaScript and a server side JavaScript - we are looking only at the
client side
- JavaScript can be embedded
in either the header or the body.
The browser uses its JavaScript interpreter when it encounters
JavaScript. One concept of when to
put things in the head and when to put things in the body is based on
purpose. Code that reacts to users
or produces on demand such as functions are usually in the head. Things that are interpreted as they are
encountered and only once are in the body. They are handled as the drop-though of the HTML document
progresses. Things in the head are not interpreted when they are
encountered - they are acted on when something in the body demands their
execution.
- JavaScript does not
have classes and therefore cannot be an object-oriented language. It does have objects and the objects
can have properties and methods.
- It is best to use
<script language = "JavaScript"> rather than rely on the
fact that JavaScript is the default script.
- JavaScript has five
primitive types: Number, String, Boolean, Undefined and Null. There are
predefined objects of types Number, String and Boolean which are called
wrapper types and provide properties and methods for the objects.
- JavaScript is
dynamically typed meaning that a variable type is determined by the
interpreter. You can declare a
variable either by using var to declare it or by simply assigning it a
value.
- JavaScript supports
unary operators (although I don't use them)
- JavaScript does
implicit type conversions attempting to covert to the required type..
- You can use the length
property with the string object to determine the length of a string. There are other string methods
including toLowerCase, toUpperCase, substring, charAt, indexOf.
var thename="Smith/John";
var thelen = thename.length;
thename.charAt(5) will return the / (note that
countin starts at 0)
thename.indexOf('/') will return 5
thename.substring(6,9) will return John
thename.toUpperCase will return SMITH/JOHN
thename.toLowerCase will return smith/john
- We have used the alert,
there is also a confirm that shows an OK and a Cancel and saves the
results as true/false: var ans = confirm("Is this
correct?");
- Remember when testing a
condition, is equal to is == and is not equal to is !=. Also when doing compound conditions,
AND is &&, OR is || and NOT is !
- JavaScript has a switch
statement that does the case structure (compare to C)
- The dowhile places the
completion test at the end of the loop construct so the code in the loop
is always executed at least once.
- JavaScript arrays have
dynamic length.
- Array objects can be
created using new or using a literal array value.
var schedule = newArray("CIS17",
"CIS44", "ENG11", "MTH31");
var schedule = newArray(5);
var schedule = ["CIS53",
"CIS31", "HST21"]
- Methods collection
for an array
schedule.sort(); elements become string and are
sorted
var rslt = schedule.join(" / "); means that
the courses in the schedule are strung together with a / between them and stored in rslt.
schedule.concat() will add elements to the array
var partschedule = schedule.slice(1,2) will take out
starting with element 1 through element2 (first element in array is element0)
There are others.
- Subprograms in
JavaScript are functions. Note
that there is a return statement which can include information returned by
the function. Information can be
passed to the function with parameters.
Depending on the definition of data information may still be
available as a result of the function even if data was not returned. When you look at functions on the web,
the return is frequently not in evidence although it is the prescribed way
of returning data.
- If you do not use var
when declaring a variable then the variable gets implicitly declared when
it is first encountered which means that it has global scope (implicitly
declared variables have global scope even if the declaration is in a
function). This is used frequently
in the web functions you find on a variety of web sites even though
programmers would prefer more formal declarations. If you explicitly declare a variable
outside a function then that variable also has global scope (preferred
way). A variable that is
explicitly declared within a function has local scope (preferred
way). Note that if a variable is
defined both globally and locally, the local wins.
- The parameter passing
method used by JavaScript is the pass by value method.