JavaScript: The Definitive Guide Notes Essentials: case-sensitive, untyped Chapter 1, Introduction to JavaScript • JS is untyped • Objects are more like associative arrays (hashes in Perl) • JS is a misnomer; it was originally called LiveScript and isn’t related to Java, however the language can interact well with Java applets for drawing and network functions Chapter 2, Lexical Structure • Lexical structure: low-level language syntax • Core JS is case-sensitive, but client-side JS has its exception with IE3 • White space is ignored • Commenting: o // for one line o /* */ for multiple lines • Literals (a data value) o “”, '’, 12, true, false, /javascript/cgi (reg expression), null, {}, [ ] • Identifiers (a name, typically of a variable or function) o May begin with a letter, _, or $ o May not begin with a number, or be reserved keywords • Reserved words: pg. 27 Chapter 3, Data Types and Values • Objects (including functions and arrays), Numbers, Strings, Boolean, and null and undefined (both represent a single value) • An object can be thought of as a container of other data types • Other specialized objects: Date class, RegExp class, and the Error class • Numbers are 64-bit floating-point values, according to IEEE 754 • Hex values begin with 0x or 0X which leads with the digit 0 • Octal literals (base-8 format) begin with a 0 followed by three digits between 0-7 • Floating-point literal syntax [digits][.digits][(E|e)][(+|-)digits] • Escape characters: pg. 35 • Working with Strings: o aString.length o s.charAt(n) is the nth character in s o s.substring(a,b) is a string composed of the letters between the ath and bth characters in string s o s.indexOf(‘a’) returns the position of the first instance of the a character in string s
• • • • • • • •
o stings can sometimes be accessed like arrays, so to get the 5th char of string s, use s[5], but avoid this syntax because it is not supported by ECMAScript v3 Unnamed functions can be embedded as literal data types using function literals, sometimes called Lambda Functions o var square = function(x) { return x*x; } You can also define functions using the function constructor: var square = new function(“x”, “return x*x;”); however this technique may result in functions which are less efficient Object literals o var point = { x:1.2, y:23.1 }; o in the example above, point.x is set to 1.2 Arrays o Constructor function: array(), var a = new array(); a[0] = 5; o Literal: a = [1, “javascript”, true]; Undefined variables o var undefined; Date object o All dates are zero based, so for example December would be month 11 RegEx o Text between //’s Primitive Data Type Wrappers o There are three primitive data types: strings, numbers, and Booleans o The primitive types cannot be operated on like objects, so whenever they are needed to be accessed using object notation, a copy of the data is made into an object, a wrapper object, so that the data contained in the primitive can be interfaced o To create a primitive string, use var a = “this is a string”; o To create a string wrapper, use var a = new String(“this is a string”); o The distinction between a primitive and a wrapper is slight enough, and the wrapper conversion process efficient enough, that it should not be taken into consideration when developing
Chapter 4, Variables • JavaScript is dynamically typed, so it changes the types of variables automatically and as needed (like when appending a number to a string, the number gets converted to a string before being appended) • Declaration: var i; • Multiple declarations: var i, sum; • JavaScript with automatically (implicitly) declare an object if “var” is not used • Implicitly declared variables are assumed to be global variables, to make a local variable for use in a single function, you must use the var declaration
• •
•
• • • • • •
Best Practice: declare all variables at the beginning of a function, because the scope of a local function is the entire function body, not just the part of the function following the declaration Undefined v. Unassigned o There are 2 kinds of undefined variable o Undeclared: the variable has never been declared, i.e. created o Unassigned: the variable has no set value Primitive & Reference types o Primitive types numbers, Boolean, null, undefined Fixed size in memory o Reference types objects, arrays, functions Variable size in memory Reference variables are called so because a primitive type variable is used to store a reference to the data, this can be a pointer or memory address where the code is stored Making a copy of a reference type variable will make a copy of the pointer. If you then change any of the copied (or original) variables, you’re effectively altering the referenced data and not the original pointer. This means that altering one copy will alter all copies of the reference variable o Of strings Strings are immutable, meaning that they cannot be changed – in this sense strings are neither reference or primitive, as they behave a bit like both Garbage collection in JavaScript is automatic – memory is freed when it is determined to be unreachable, meaning that there are no references or pointers to the memory allocated to store the variable data Variables are basically the same as object properties When JavaScript starts up it creates a global object, so that any global variable you declare is actually a property of it “window” is a global object, but is self-referential so there is no need to type this.window Local variables are properties of the call object which lasts only so long as needed, determined by the variable scope (a call object is created for each function, and is destroyed after each function is run) Execution context o Each time a function is run, an execution context is created to store local variables and such (more details are available on page 54, regarding the creation of global variables for each execution context like in client-side implementations) o Includes a scope chain which is a list, or chain, of objects JavaScript checks, in order, for variable values (p. 55)
Chapter 5, Expressions and Operators (Review) • == is equality, and === is for indentity • Identity checks for identical operands • Conditional operator: ? : o This operator works like so: Boolean value or conditional (a) ? code to execute if a is true : code to execute if a is false o The operator is shorthand for a simple if/then statement Chapter 6, Statements • Use {} to make multiple statements act as a single statement where they’re expected, like in functions •