Table of Contents
Preface; Introduction; More Code, Fewer Words; Exhaustive Code and Repetition; Color-Coding Conventions; jsFiddle, JS Bin, and Firebug lite-dev; Conventions Used in This Book; Using Code Examples; Safari® Books Online; How to Contact Us; About the Author; About the Technical Editors; Chapter 1: JavaScript Objects; 1.1 Creating Objects; 1.2 JavaScript Constructors Construct and Return Object Instances; 1.3 The JavaScript Native/Built-In Object Constructors; 1.4 User-Defined/Non-Native Object Constructor Functions; 1.5 Instantiating Constructors Using the new Operator; 1.6 Creating Shorthand/Literal Values from Constructors; 1.7 Primitive (a.k.a. Simple) Values; 1.8 The Primitive Values null, undefined, “string”, 10, true, and false Are Not Objects; 1.9 How Primitive Values Are Stored/Copied in JavaScript; 1.10 Primitive Values Are Equal by Value; 1.11 The String, Number, and Boolean Primitive Values Act Like Objects When Used Like Objects; 1.12 Complex (a.k.a. Composite) Values; 1.13 How Complex Values Are Stored/Copied in JavaScript; 1.14 Complex Objects Are Equal by Reference; 1.15 Complex Objects Have Dynamic Properties; 1.16 The typeof Operator Used on Primitive and Complex Values; 1.17 Dynamic Properties Allow for Mutable Objects; 1.18 All Constructor Instances Have Constructor Properties that Point to Their Constructor Function; 1.19 Verify that an Object Is an Instance of a Particular Constructor Function; 1.20 An Instance Created From a Constructor Can Have Its Own Independent Properties (Instance Properties); 1.21 The Semantics of “JavaScript Objects” and “Object() Objects”; Chapter 2: Working with Objects and Properties; 2.1 Complex Objects Can Contain Most of the JavaScript Values as Properties; 2.2 Encapsulating Complex Objects in a Programmatically Beneficial Way; 2.3 Getting/Setting/Updating an Object’s Properties Using Dot Notation or Bracket Notation; 2.4 Deleting Object Properties; 2.5 How References to Object Properties Are Resolved; 2.6 Using hasOwnProperty, Verify That an Object Property Is Not From the Prototype Chain; 2.7 Checking If an Object Contains a Given Property Using the in Operator; 2.8 Enumerate (Loop Over) an Object’s Properties using the for in Loop; 2.9 Host Objects versus Native Objects; 2.10 Enhancing and Extending Objects with Underscore.js; Chapter 3: Object(); 3.1 Conceptual Overview of Using Object() Objects; 3.2 Object() Parameters; 3.3 Object() Properties and Methods; 3.4 Object() Object Instance Properties and Methods; 3.5 Creating Object() Objects Using “Object Literals”; 3.6 All Objects Inherit From Object.prototype; Chapter 4: Function(); 4.1 Conceptual Overview of Using Function() Objects; 4.2 Function() Parameters; 4.3 Function() Properties and Methods; 4.4 Function Object Instance Properties and Methods; 4.5 Functions Always Return a Value; 4.6 Functions Are First-Class Citizens (Not Just Syntax but Values); 4.7 Passing Parameters to a Function; 4.8 this and arguments Values Available To All Functions; 4.9 The arguments.callee Property; 4.10 The Function Instance length Property and arguments.length; 4.11 Redefining Function Parameters; 4.12 Return a Function Before It Is Done (Cancel Function Execution); 4.13 Defining a Function (Statement, Expression, or Constructor); 4.14 Invoking a Function [Function, Method, Constructor, or call() and apply()]; 4.15 Anonymous Functions; 4.16 Self-Invoking Function Expression; 4.17 Self-Invoking Anonymous Function Statements; 4.18 Functions Can Be Nested; 4.19 Passing Functions to Functions and Returning Functions from Functions; 4.20 Invoking Function Statements Before They Are Defined (Function Hoisting); 4.21 A Function Can Call Itself (Recursion); Chapter 5: The Head/Global Object; 5.1 Conceptual Overview of the Head Object; 5.2 Global Functions Contained Within the Head Object; 5.3 The Head Object versus Global Properties and Global Variables; 5.4 Referring to the Head Object; 5.5 The Head Object Is Implied and Typically Not Referenced Explicitly; Chapter 6: The this Keyword; 6.1 Conceptual Overview of this and How It Refers to Objects; 6.2 How Is the Value of this Determined?; 6.3 The this Keyword Refers to the Head Object in Nested Functions; 6.4 Working Around the Nested Function Issue by Leveraging the Scope Chain; 6.5 Controlling the Value of this Using call() or apply(); 6.6 Using the this Keyword Inside a User-Defined Constructor Function; 6.7 The this Keyword Inside a Prototype Method Refers to a Constructor Instance; Chapter 7: Scope and Closures; 7.1 Conceptual Overview of JavaScript Scope; 7.2 JavaScript Does Not Have Block Scope; 7.3 Use var Inside Functions to Declare Variables and Avoid Scope Gotchas; 7.4 The Scope Chain (Lexical Scoping); 7.5 The Scope Chain Lookup Returns the First Found Value; 7.6 Scope Is Determined During Function Definition, not Invocation; 7.7 Closures Are Caused by the Scope Chain; Chapter 8: Function Prototype Property; 8.1 Conceptual Overview of the Prototype Chain; 8.2 Why Care About the prototype Property?; 8.3 Prototype Is Standard on All function() Instances; 8.4 The Default prototype Property Is an Object() Object; 8.5 Instances Created From a Constructor Function are Linked to the Constructor’s prototype Property; 8.6 Last Stop in the prototype Chain is Object.prototype; 8.7 The prototype Chain Returns the First Property Match It Finds in the Chain; 8.8 Replacing the prototype Property with a New Object Removes the Default Constructor Property; 8.9 Instances That Inherit Properties from the Prototype Will Always Get the Latest Values; 8.10 Replacing the prototype Property with a New Object Does Not Update Former Instances; 8.11 User-Defined Constructors Can Leverage the Same Prototype Inheritance as Native Constructors; 8.12 Creating Inheritance Chains (the Original Intention); Chapter 9: Array(); 9.1 Conceptual Overview of Using Array() Objects; 9.2 Array() Parameters; 9.3 Array() Properties and Methods; 9.4 Array Object Instance Properties and Methods; 9.5 Creating Arrays; 9.6 Adding and Updating Values in Arrays; 9.7 Length versus Index; 9.8 Defining Arrays with a Predefined Length; 9.9 Setting Array Length can Add or Remove Values; 9.10 Arrays Containing Other Arrays (Multidimensional Arrays); 9.11 Looping Over an Array, Backwards and Forwards; Chapter 10: String(); 10.1 Conceptual Overview of Using the String() Object; 10.2 String() Parameters; 10.3 String() Properties and Methods; 10.4 String Object Instance Properties and Methods; Chapter 11: Number(); 11.1 Conceptual Overview of Using the Number() Object; 11.2 Integers and Floating-Point Numbers; 11.3 Number() Parameters; 11.4 Number() Properties; 11.5 Number Object Instance Properties and Methods; Chapter 12: Boolean(); 12.1 Conceptual Overview of Using the Boolean() Object; 12.2 Boolean() Parameters; 12.3 Boolean() Properties and Methods; 12.4 Boolean Object Instance Properties and Methods; 12.5 Non-Primitive False Boolean Objects Convert to true; 12.6 Certain Things Are false, Everything Else Is true; Chapter 13: Working with Primitive String, Number, and Boolean Values; 13.1 Primitive/Literal Values Are Converted to Objects When Properties Are Accessed; 13.2 You Should Typically Use Primitive String, Number, and Boolean Values; Chapter 14: Null; 14.1 Conceptual Overview of Using the null Value; 14.2 typeof Returns null Values as “object”; Chapter 15: Undefined; 15.1 Conceptual Overview of the undefined Value; 15.2 JavaScript ECMAScript 3 Edition (and Later) Declares the undefined Variable in the Global Scope; Chapter 16: Math Function; 16.1 Conceptual Overview of the Built-In Math Object; 16.2 Math Properties and Methods; 16.3 Math Is Not a Constructor Function; 16.4 Math Has Constants You Cannot Augment/Mutate; Review; Conclusion; Colophon;