You are previewing JavaScript: The Good Parts.

JavaScript: The Good Parts

Cover of JavaScript: The Good Parts by Douglas Crockford Published by O'Reilly Media, Inc.
  1. JavaScript: The Good Parts
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Preface
      1. Conventions Used in This Book
      2. Using Code Examples
      3. Safari® Books Online
      4. How to Contact Us
      5. Acknowledgments
    4. 1. Good Parts
      1. Why JavaScript?
      2. Analyzing JavaScript
      3. A Simple Testing Ground
    5. 2. Grammar
      1. Whitespace
      2. Names
      3. Numbers
      4. Strings
      5. Statements
      6. Expressions
      7. Literals
      8. Functions
    6. 3. Objects
      1. Object Literals
      2. Retrieval
      3. Update
      4. Reference
      5. Prototype
      6. Reflection
      7. Enumeration
      8. Delete
      9. Global Abatement
    7. 4. Functions
      1. Function Objects
      2. Function Literal
      3. Invocation
      4. Arguments
      5. Return
      6. Exceptions
      7. Augmenting Types
      8. Recursion
      9. Scope
      10. Closure
      11. Callbacks
      12. Module
      13. Cascade
      14. Curry
      15. Memoization
    8. 5. Inheritance
      1. Pseudoclassical
      2. Object Specifiers
      3. Prototypal
      4. Functional
      5. Parts
    9. 6. Arrays
      1. Array Literals
      2. Length
      3. Delete
      4. Enumeration
      5. Confusion
      6. Methods
      7. Dimensions
    10. 7. Regular Expressions
      1. An Example
      2. Construction
      3. Elements
    11. 8. Methods
    12. 9. Style
    13. 10. Beautiful Features
    14. A. Awful Parts
      1. Global Variables
      2. Scope
      3. Semicolon Insertion
      4. Reserved Words
      5. Unicode
      6. typeof
      7. parseInt
      8. +
      9. Floating Point
      10. NaN
      11. Phony Arrays
      12. Falsy Values
      13. hasOwnProperty
      14. Object
    15. B. Bad Parts
      1. ==
      2. with Statement
      3. eval
      4. continue Statement
      5. switch Fall Through
      6. Block-less Statements
      7. ++ −−
      8. Bitwise Operators
      9. The function Statement Versus the function Expression
      10. Typed Wrappers
      11. new
      12. void
    16. C. JSLint
      1. Undefined Variables and Functions
      2. Members
      3. Options
      4. Semicolon
      5. Line Breaking
      6. Comma
      7. Required Blocks
      8. Forbidden Blocks
      9. Expression Statements
      10. for in Statement
      11. switch Statement
      12. var Statement
      13. with Statement
      14. =
      15. == and !=
      16. Labels
      17. Unreachable Code
      18. Confusing Pluses and Minuses
      19. ++ and −−
      20. Bitwise Operators
      21. eval Is Evil
      22. void
      23. Regular Expressions
      24. Constructors and new
      25. Not Looked For
      26. HTML
      27. JSON
      28. Report
    17. D. Syntax Diagrams
    18. E. JSON
      1. JSON Syntax
      2. Using JSON Securely
      3. A JSON Parser
    19. Index
    20. About the Author
    21. Colophon
    22. SPECIAL OFFER: Upgrade this ebook with O’Reilly
O'Reilly logo

Functional

One weakness of the inheritance patterns we have seen so far is that we get no privacy. All properties of an object are visible. We get no private variables and no private methods. Sometimes that doesn't matter, but sometimes it matters a lot. In frustration, some uninformed programmers have adopted a pattern of pretend privacy. If they have a property that they wish to make private, they give it an odd-looking name, with the hope that other users of the code will pretend that they cannot see the odd looking members. Fortunately, we have a much better alternative in an application of the module pattern.

We start by making a function that will produce objects. We will give it a name that starts with a lowercase letter because it will not require the use of the new prefix. The function contains four steps:

  1. It creates a new object. There are lots of ways to make an object. It can make an object literal, or it can call a constructor function with the new prefix, or it can use the Object.create method to make a new instance from an existing object, or it can call any function that returns an object.

  2. It optionally defines private instance variables and methods. These are just ordinary vars of the function.

  3. It augments that new object with methods. Those methods will have privileged access to the parameters and the vars defined in the second step.

  4. It returns that new object.

Here is a pseudocode template for a functional constructor (boldface text added for emphasis):

var constructor ...

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