2. Background
Netscape Navigator
Designed by Brendon Eich in 1995
Built to replace Java for the web and be “easy to use”
Principles from Java (syntax), Self (prototypes) & Scheme (functional)
Rushed to market with good, bad & questionable design choices
Microsoft created Jscript, a compatible dialect to avoid trademark issues
ECMA
Standardized by ECMA (European Computer Manufacturers Association)
Called ECMAScript between Netscape & Microsoft
Latest version 5 includes “strict mode” for more thorough error checking
3. Basic Types
number
Only 64-bit floating point
No integers, longs, doubles, signed or unsigned to complicate matters
Associative Law does not hold for some values
(a + b) + c === a + (b + c)
string
0 or more 16-bit Unicode (UCS-2) characters. No character type
boolean
true & false
4. Objects
Object Literals
Dynamic (not based on pre-defined class) collection of properties
Property names must be a unique string within the object
Basic Operations – Get, Set & Delete
object.name | object[name]
object.name = value | object[name] = value
delete object.name | delete object[name]
Property
Named collection of attributes
ES5 added functionality to define properties and added Get, Set functions
Attributes: value, writable, configurable, enumerable, get, set
6. Object Types
Arrays
Special object with index as property names (not associative)
Has properties like: length, sort(), filter()
RegEx
Regular expression object
2 primary methods exec() returns a match and test() returns boolean
Functions
An object which can be invoked with parentheses ()
9. Functions
Composition
Made up of 4 parts: function, name (optional), parameters, set of statements
2 additional parameters: arguments object & this (invocation context)
Invocation
Method – this is the local object
Function – this is the global object
Apply – this is passed as a parameter
Constructor – this is the new object
Calling
Assigning less parameters will make them undefined
11. Inheritance with Prototypes
Prototype with functions
All functions have a property prototype as a base object
Use new keyword with function invocation to create new objects
Constructor functions should start with a capital letter
Inheritance
If object doesn’t have a property look in base object
Value defaults to undefinedWritable defaults to falseConfigurable defaults to falseEnumerable defaults to falseGet defaults to undefinedSet defaults to undefined
Method: local objectFunction: global object // mistake, should be outer function this objectthat work-aroundConstructor: new objectApply: choose, list of parameters