JavaScript for PHP Developers

Book description

If you want to significantly expand your web development skills beyond PHP, this practical, hands-on book teaches you ECMAScript—the core JavaScript language—from the ground up. You’ll discover some similarities between JavaScript and PHP, such as conditions and loops, but the primary focus is on JavaScript’s unique object creation, classes, prototypes, and inheritance.

JavaScript knowledge is essential for working with today’s Web, whether you’re building applications for the client, the server, or for mobile use—and your PHP experience gives you a head start. This book will help you become fluent with JavaScript quickly, and then serve as a handy reference once you start coding.

  • Explore JavaScript syntax, including variables, arrays, loops, and conditions
  • Learn how functions are important in JavaScript—and why they’re actually objects
  • Delve into JavaScript’s object-oriented features, including prototypes, code reuse, and inheritance
  • Examine the built-in API and explore its global functions, properties, and objects
  • Learn about updates in ECMAScript5, the latest version of the standard
  • Use common design patterns to organize your code in large applications

Publisher resources

View/Submit Errata

Table of contents

  1. JavaScript for PHP Developers
  2. Acknowledgments
  3. Dedication
  4. Preface
    1. Conventions Used in This Book
    2. Using Code Examples
    3. Safari® Books Online
    4. How to Contact Us
  5. 1. Introduction
    1. Scope of This Book
    2. The Language
    3. Learning Environment
      1. Browsers
      2. JavaScriptCore
      3. Node.js and Rhino
    4. Longer Examples
    5. Let’s Get Started
  6. 2. JavaScript Syntax
    1. Variables
      1. What Does $ Do in JavaScript?
    2. Values
      1. typeof Introspection
      2. null and undefined
    3. Arrays
      1. Associative Arrays
    4. Conditions
      1. Ternary Operator
      2. Strict Comparison
      3. switch
      4. try-catch
    5. while and for Loops
    6. for-in Loops
    7. Miscellaneous Operators
      1. in
      2. String Concatenation
      3. Type Casting
      4. void
      5. Comma Operator
  7. 3. Functions
    1. Default Parameters
    2. Any Number of Arguments
    3. arguments.length Trick
    4. Return Values
    5. Functions Are Objects
    6. A Different Syntax
    7. Scope
    8. Hoisting
      1. Hoisting Functions
    9. Closures
      1. Closures in PHP
    10. Scope Chain
      1. The Scope Chain in the WebKit Console
      2. Retaining the Scope
      3. Retaining References, Not Values
      4. Closures in a Loop
      5. Exercise: onclick Loop
    11. Immediate Functions
      1. Initialization
      2. Privacy
    12. Passing and Returning Functions
      1. Callbacks Are Not Strings
  8. 4. Object-Oriented Programming
    1. Constructors and Classes
      1. Returning Objects
      2. More on This
      3. Enforcing Constructors
    2. Prototypes
    3. Object Literals
      1. Accessing Properties
      2. Confusing Dots
      3. Methods in Object Literals
    4. Fancy Arrays
    5. Own Properties
      1. __proto__
      2. this or prototype
    6. Inheritance
      1. Inheritance via the Prototype
      2. Inheritance via Copying Properties
      3. Beget Object
      4. “Classical” extend()
      5. Borrowing Methods
      6. Conclusion
  9. 5. The Built-In API
    1. The Global Object
    2. Global Properties
    3. Global Functions
      1. Numbers
      2. Encoding URLs
    4. Built-In Constructors
      1. Object
        1. toString()
        2. toLocaleString()
        3. valueOf()
        4. hasOwnProperty()
        5. propertyIsEnumerable()
        6. isPrototypeOf()
        7. constructor
      2. Array
        1. length
        2. push()
        3. pop()
        4. unshift()
        5. shift()
        6. concat()
        7. sort()
        8. slice()
        9. splice()
        10. reverse()
        11. join()
      3. RegExp
        1. test() and properties
        2. exec()
      4. Function
        1. Function properties
      5. String
        1. substring()
        2. localeCompare()
        3. split()
        4. search()
        5. replace()
        6. match()
      6. Number
      7. Boolean
      8. Math
      9. Error
      10. Date
    5. Constructors Review
  10. 6. ECMAScript 5
    1. Strict Mode
    2. Property Attributes
    3. New Object APIs
      1. Object.create()
      2. Object.getOwnPropertyDescriptor()
      3. Object.defineProperty() and Object.defineProperties()
      4. Restricting Object Mutations
      5. Looping Alternatives
      6. Object.getPrototypeOf()
    4. Array Additions
      1. Array.isArray()
      2. indexOf() and lastIndexOf()
      3. Walking the Array Elements
      4. Filtering
      5. Testing the Array Content
      6. Map/Reduce
    5. String Trimming
    6. New in Date
    7. Function.prototype.bind()
    8. JSON
    9. Shims
  11. 7. JavaScript Patterns
    1. Private Properties
      1. Private Methods
      2. Exposing Private Objects
      3. Returning Private Arrays
      4. Deep Copy via JSON
    2. Revealing Pattern
    3. Constants
    4. Namespaces
    5. Modules
    6. CommonJS Modules
      1. Defining a CommonJS Module
      2. Using a CommonJS Module
      3. Using an Agnostic Module
    7. AMD
    8. Design Patterns
      1. Singleton
        1. Singletons with constructors
      2. Factory
      3. Decorator
        1. Decoration API
        2. Adding decorators
        3. decorate() and get()
    9. Documentation and Testing
      1. Manual
      2. Documenting Your Code
      3. Unit Testing
      4. JSLint
      5. Linting This Book
  12. Index
  13. About the Author
  14. Colophon
  15. Copyright

Product information

  • Title: JavaScript for PHP Developers
  • Author(s): Stoyan Stefanov
  • Release date: April 2013
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449370312