JavaScript Fundamentals
Why JavaScript?
JavaScript is an important language because it is the language of the web browser. Its
association with the browser makes it one of the most popular programming languages
in the world.
Analyzing JavaScript
JavaScript is built on some very good ideas and a few very bad ones.
The very good ideas include functions, loose typing, dynamic objects, and an expressive
object literal notation. The bad ideas include a programming model based on
global variables.
Grammar2
A Simple Testing Ground
<html>
<body>
<script src="program.js">
</script>
</body>
</html>
Whitespace
var that = this;
the space between var and that cannot be removed, but the other spaces can be
removed.
Strings
A string literal can be wrapped in single quotes or double quotes. It can contain zero
or more characters.
Strings have methods
'cat'.toUpperCase( ) === 'CAT'
Statements
if(a){return true;}else{return false;}
Operator precedence
. [] ( ) Refinement and invocation
delete new typeof + — ! Unary operators
* / % Multiplication, division, modulo
+ — Addition/concatenation, subtraction
>= <= > < Inequality
=== !== Equality
&& Logical and
|| Logical or
?: Ternary
Literals
Object literals are a convenient notation for specifying new objects.
Functions
A function literal defines a function value. It can have an optional name that it can
use to call itself recursively
Objects
Object Literals
Retrieval
Values can be retrieved from an object by wrapping a string expression in a [ ] suffix.
If the string expression is a constant, and if it is a legal JavaScript name and not a
reserved word, then the . notation can be used instead. The . notation is preferred
because it is more compact and it reads betterReference
Reflection
Enumeration
Delete
Global Abatement
One way to minimize the use of global variables is to create a single global variable
for your application:
Functions
Function Literal
The Method Invocation PatternWhen a function is stored as a property of an object, we call it a method.
The Function Invocation Pattern
When a function is not the property of an object, then it is invoked as a function:
The Constructor Invocation Pattern
Arguments
Exceptions
Recursion
A recursive function is a function that calls itself, either directly or indirectly.ScopeClosure (functions in functions)We are not assigning a function to myObject. We are assigning the result of invoking
that function. Notice the ( ) on the last line.ModuleCascade
Memoization
wrong: