CSCI 446 Unit #3 Part I
Preparation:
Walk through the
basic JavaScript tutorials.
Sections that are not mentioned can be skipped for now,
and used as a reference if needed. OR, you may want to briefly skim
all the sections, just to get an idea of the language features.
- JS Introduction
- For these examples, the JS is simply embedded in
onclick
- JS Where To
- As with CSS, there are multiple options for where to place JS
- What advantage is there to putting a script at the bottom of
the
body
, rather than in the head
?
- For this course, most of the JS will be in external files
- JS Output
-
- Just as you can use
cout
statements to help
debug C++ programs, in JS you can use alert
and/or
console.log
for this purpose. These are also used
extensively when learning, to see the effect of statements.
- JS Syntax
- Syntax is pretty similar to other languages
- Be sure to use camelCase for variable names
- JS Variables
- When would a variable be
undefined
?
- Describe two different uses for
+
?
- Skim JS Operators.
- Skim JS Data Types
- NOTE: JS has dynamic data types. If you've never heard
that term, try putting
document.writeln(x);
between
the assignment statements in the
"Tryit" example of "JavaScript Types are Dynamic."
- Read JS Functions
- Skim the beginning of this section, which just explains what a
function is (you all know that!).
- Read "The () Operator Invokes the Function" section. Of course it
does. But what happens if you leave off the ()? NOTE! JS has
first-class functions, which means the function
itself can be returned. You have not seen first-class
functions in your C++ or Java coursework, so this is a new concept.
We'll return to this idea later.
- Read JS Objects
- Objects in JS are just name:pair values (basically just a hash map)
- Is it a good idea to use
new
to create elements like
String, Number, Array?
- Skim JS Strings
- Can String objects be compared?
- Is it a good idea to access elements of a string as an array? e.g.,
myStr[0]
- Skim JS Numbers
- Skim JS Arrays
- What does
push
do?
- If there are 3 elements in array
stuff
, is this valid:
stuff[10] = 5;
? If yes, would it be good practice?
- Does JS have associative arrays (hashes)?
- How would you decide whether to store data as an array or object?
- Read JS Booleans
- Read JS Comparisons
- There are a number of aspects of JS that experienced programmers
consider "bad" design. How JS
does comparisons is one of them. Try these examples to see some
of the pitfalls.
- Read JS Hoisting
- Are both initializations and declarations hoisted?
- What's a good practice to avoid bugs related to hoisting?
- Skim JS Strict through
JS Mistakes
- These sections are important, but many of the guidelines were
discussed in prior sections
- Please use camelCase for your variable names