You are previewing JavaScript Enlightenment.

JavaScript Enlightenment

Cover of JavaScript Enlightenment by Cody Lindley Published by O'Reilly Media, Inc.
  1. JavaScript Enlightenment
  2. Preface
    1. Introduction
      1. Why Did I Write This Book?
      2. Who Should Read This Book?
      3. Why JavaScript 1.5 and ECMAScript 3 Edition?
      4. Why Didn’t I Cover the Date(), Error(), and RegEx() Objects?
    2. More Code, Fewer Words
    3. Exhaustive Code and Repetition
    4. Color-Coding Conventions
    5. jsFiddle, JS Bin, and Firebug lite-dev
    6. Conventions Used in This Book
    7. Using Code Examples
    8. Safari® Books Online
    9. How to Contact Us
    10. About the Author
    11. About the Technical Editors
      1. Michael Richardson
      2. Kyle Simpson
      3. Nathan Smith
      4. Ben Nadel
      5. Ryan Florence
      6. Nathan Logan
  3. 1. JavaScript Objects
    1. Creating Objects
    2. JavaScript Constructors Construct and Return Object Instances
    3. The JavaScript Native/Built-In Object Constructors
    4. User-Defined/Non-Native Object Constructor Functions
    5. Instantiating Constructors Using the new Operator
    6. Creating Shorthand/Literal Values from Constructors
    7. Primitive (a.k.a. Simple) Values
    8. The Primitive Values null, undefined, “string”, 10, true, and false Are Not Objects
    9. How Primitive Values Are Stored/Copied in JavaScript
    10. Primitive Values Are Equal by Value
    11. The String, Number, and Boolean Primitive Values Act Like Objects When Used Like Objects
    12. Complex (a.k.a. Composite) Values
    13. How Complex Values Are Stored/Copied in JavaScript
    14. Complex Objects Are Equal by Reference
    15. Complex Objects Have Dynamic Properties
    16. The typeof Operator Used on Primitive and Complex Values
    17. Dynamic Properties Allow for Mutable Objects
    18. All Constructor Instances Have Constructor Properties that Point to Their Constructor Function
    19. Verify that an Object Is an Instance of a Particular Constructor Function
    20. An Instance Created From a Constructor Can Have Its Own Independent Properties (Instance Properties)
    21. The Semantics of “JavaScript Objects” and “Object() Objects”
  4. 2. Working with Objects and Properties
    1. Complex Objects Can Contain Most of the JavaScript Values as Properties
    2. Encapsulating Complex Objects in a Programmatically Beneficial Way
    3. Getting/Setting/Updating an Object’s Properties Using Dot Notation or Bracket Notation
    4. Deleting Object Properties
    5. How References to Object Properties Are Resolved
    6. Using hasOwnProperty, Verify That an Object Property Is Not From the Prototype Chain
    7. Checking If an Object Contains a Given Property Using the in Operator
    8. Enumerate (Loop Over) an Object’s Properties using the for in Loop
    9. Host Objects versus Native Objects
    10. Enhancing and Extending Objects with Underscore.js
  5. 3. Object()
    1. Conceptual Overview of Using Object() Objects
    2. Object() Parameters
    3. Object() Properties and Methods
    4. Object() Object Instance Properties and Methods
    5. Creating Object() Objects Using “Object Literals”
    6. All Objects Inherit From Object.prototype
  6. 4. Function()
    1. Conceptual Overview of Using Function() Objects
    2. Function() Parameters
    3. Function() Properties and Methods
    4. Function Object Instance Properties and Methods
    5. Functions Always Return a Value
    6. Functions Are First-Class Citizens (Not Just Syntax but Values)
    7. Passing Parameters to a Function
    8. this and arguments Values Available To All Functions
    9. The arguments.callee Property
    10. The Function Instance length Property and arguments.length
    11. Redefining Function Parameters
    12. Return a Function Before It Is Done (Cancel Function Execution)
    13. Defining a Function (Statement, Expression, or Constructor)
    14. Invoking a Function [Function, Method, Constructor, or call() and apply()]
    15. Anonymous Functions
    16. Self-Invoking Function Expression
    17. Self-Invoking Anonymous Function Statements
    18. Functions Can Be Nested
    19. Passing Functions to Functions and Returning Functions from Functions
    20. Invoking Function Statements Before They Are Defined (Function Hoisting)
    21. A Function Can Call Itself (Recursion)
  7. 5. The Head/Global Object
    1. Conceptual Overview of the Head Object
    2. Global Functions Contained Within the Head Object
    3. The Head Object versus Global Properties and Global Variables
    4. Referring to the Head Object
    5. The Head Object Is Implied and Typically Not Referenced Explicitly
  8. 6. The this Keyword
    1. Conceptual Overview of this and How It Refers to Objects
    2. How Is the Value of this Determined?
    3. The this Keyword Refers to the Head Object in Nested Functions
    4. Working Around the Nested Function Issue by Leveraging the Scope Chain
    5. Controlling the Value of this Using call() or apply()
    6. Using the this Keyword Inside a User-Defined Constructor Function
    7. The this Keyword Inside a Prototype Method Refers to a Constructor Instance
  9. 7. Scope and Closures
    1. Conceptual Overview of JavaScript Scope
    2. JavaScript Does Not Have Block Scope
    3. Use var Inside Functions to Declare Variables and Avoid Scope Gotchas
    4. The Scope Chain (Lexical Scoping)
    5. The Scope Chain Lookup Returns the First Found Value
    6. Scope Is Determined During Function Definition, not Invocation
    7. Closures Are Caused by the Scope Chain
  10. 8. Function Prototype Property
    1. Conceptual Overview of the Prototype Chain
    2. Why Care About the prototype Property?
    3. Prototype Is Standard on All function() Instances
    4. The Default prototype Property Is an Object() Object
    5. Instances Created From a Constructor Function are Linked to the Constructor’s prototype Property
    6. Last Stop in the prototype Chain is Object.prototype
    7. The prototype Chain Returns the First Property Match It Finds in the Chain
    8. Replacing the prototype Property with a New Object Removes the Default Constructor Property
    9. Instances That Inherit Properties from the Prototype Will Always Get the Latest Values
    10. Replacing the prototype Property with a New Object Does Not Update Former Instances
    11. User-Defined Constructors Can Leverage the Same Prototype Inheritance as Native Constructors
    12. Creating Inheritance Chains (the Original Intention)
  11. 9. Array()
    1. Conceptual Overview of Using Array() Objects
    2. Array() Parameters
    3. Array() Properties and Methods
    4. Array Object Instance Properties and Methods
    5. Creating Arrays
    6. Adding and Updating Values in Arrays
    7. Length versus Index
    8. Defining Arrays with a Predefined Length
    9. Setting Array Length can Add or Remove Values
    10. Arrays Containing Other Arrays (Multidimensional Arrays)
    11. Looping Over an Array, Backwards and Forwards
  12. 10. String()
    1. Conceptual Overview of Using the String() Object
    2. String() Parameters
    3. String() Properties and Methods
    4. String Object Instance Properties and Methods
  13. 11. Number()
    1. Conceptual Overview of Using the Number() Object
    2. Integers and Floating-Point Numbers
    3. Number() Parameters
    4. Number() Properties
    5. Number Object Instance Properties and Methods
  14. 12. Boolean()
    1. Conceptual Overview of Using the Boolean() Object
    2. Boolean() Parameters
    3. Boolean() Properties and Methods
    4. Boolean Object Instance Properties and Methods
    5. Non-Primitive False Boolean Objects Convert to true
    6. Certain Things Are false, Everything Else Is true
  15. 13. Working with Primitive String, Number, and Boolean Values
    1. Primitive/Literal Values Are Converted to Objects When Properties Are Accessed
    2. You Should Typically Use Primitive String, Number, and Boolean Values
  16. 14. Null
    1. Conceptual Overview of Using the null Value
    2. typeof Returns null Values as “object”
  17. 15. Undefined
    1. Conceptual Overview of the undefined Value
    2. JavaScript ECMAScript 3 Edition (and Later) Declares the undefined Variable in the Global Scope
  18. 16. Math Function
    1. Conceptual Overview of the Built-In Math Object
    2. Math Properties and Methods
    3. Math Is Not a Constructor Function
    4. Math Has Constants You Cannot Augment/Mutate
  19. A. Review
  20. B. Conclusion
  21. Index
  22. About the Author
  23. Colophon
  24. Copyright

Chapter 9. Array()

Conceptual Overview of Using Array() Objects

An array is an ordered list of values, typically created with the intention of looping through numerically indexed values, beginning with the index zero. What you need to know is that arrays are numerically ordered sets, versus objects, which have property names associated with values in non-numeric order. Essentially, arrays use numbers as a lookup key, while objects have user-defined property names. JavaScript does not have true associative arrays, but objects can be used to achieve the functionality of associate arrays.

Below, I store four strings in myArray that I can access using a numeric index. I compare and contrast it to an object-literal mimicking an associative array.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var myArray = ['blue', 'green', 'orange', 'red'];

console.log(myArray[0]); // logs blue using 0 index to access string in myArray

// versus

var myObject = { // a.k.a. associative array/hash, known as an object in JavaScript
   'blue': 'blue',
   'green': 'green',
   'orange': 'orange',
   'red': 'red'

console.log(myObject['blue']); // logs blue



  • Arrays can hold any type of values, and these values can be updated or deleted at any time.

  • If you need a “hash” (a.k.a. associative array), an object is the closest solution.

  • An Array() is just a special type of Object(). That is, Array() instances are basically Object() instances with a couple of extra functions (e.g., .length and ...

The best content for your career. Discover unlimited learning on demand for around $1/day.