javascript
www.it-ebooks.info www.it-ebooks.info Stoyan Stefanov JavaScript for PHP Developers www.it-ebooks.info JavaScript for PHP Developers by Stoyan Stefanov Copyright © 2013 Stoyan Stefanov. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editor: Mary Treseler Production Editor: Kara Ebrahim Copyeditor: Amanda Kersey Proofreader: Jasmine Kwityn Indexer: Meghan Jones, WordCo Indexing Cover Designer: Randy Comer Interior Designer: David Futato Illustrator: Rebecca Demarest April 2013: First Edition Revision History for the First Edition: 2013-04-24: First release See http://oreilly.com/catalog/errata.csp?isbn=9781449320195 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. JavaScript for PHP Developers, the image of an eastern gray squirrel, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐ mark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-32019-5 [LSI] www.it-ebooks.info To Nathalie, Zlatina, and Eva www.it-ebooks.info www.it-ebooks.info Table of Contents Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi 1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Scope of This Book 2 The Language 3 Learning Environment 4 Browsers 4 JavaScriptCore 5 Node.js and Rhino 6 Longer Examples 7 Let’s Get Started 7 2. JavaScript Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Variables 9 What Does $ Do in JavaScript? 10 Values 11 typeof Introspection 12 null and undefined 13 Arrays 13 Associative Arrays 15 Conditions 15 Ternary Operator 16 Strict Comparison 16 switch 18 try-catch 18 while and for Loops 19 for-in Loops 20 Miscellaneous Operators 21 v www.it-ebooks.info in 22 String Concatenation 23 Type Casting 23 void 24 Comma Operator 24 3. Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Default Parameters 27 Any Number of Arguments 28 arguments.length Trick 29 Return Values 29 Functions Are Objects 30 A Different Syntax 31 Scope 32 Hoisting 33 Hoisting Functions 34 Closures 35 Closures in PHP 35 Scope Chain 37 The Scope Chain in the WebKit Console 39 Retaining the Scope 43 Retaining References, Not Values 44 Closures in a Loop 45 Exercise: onclick Loop 47 Immediate Functions 47 Initialization 49 Privacy 49 Passing and Returning Functions 50 Callbacks Are Not Strings 52 4. Object-Oriented Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Constructors and Classes 55 Returning Objects 56 More on This 57 Enforcing Constructors 58 Prototypes 59 Object Literals 60 Accessing Properties 61 Confusing Dots 62 Methods in Object Literals 62 Fancy Arrays 63 Own Properties 64 vi | Table of Contents www.it-ebooks.info __proto__ 65 this or prototype 66 Inheritance 66 Inheritance via the Prototype 67 Inheritance via Copying Properties 68 Beget Object 69 “Classical” extend() 70 Borrowing Methods 71 Conclusion 72 5. The Built-In API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 The Global Object 73 Global Properties 74 Global Functions 74 Numbers 75 Encoding URLs 76 Built-In Constructors 76 Object 77 Array 81 RegExp 87 Function 90 String 91 Number 95 Boolean 96 Math 96 Error 97 Date 97 Constructors Review 99 6. ECMAScript 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Strict Mode 101 Property Attributes 102 New Object APIs 103 Object.create() 103 Object.getOwnPropertyDescriptor() 104 Object.defineProperty() and Object.defineProperties() 104 Restricting Object Mutations 105 Looping Alternatives 106 Object.getPrototypeOf() 107 Array Additions 107 Array.isArray() 107 indexOf() and lastIndexOf() 108 Table of Contents | vii www.it-ebooks.info Walking the Array Elements 108 Filtering 109 Testing the Array Content 109 Map/Reduce 109 String Trimming 110 New in Date 111 Function.prototype.bind() 111 JSON 112 Shims 112 7. JavaScript Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Private Properties 113 Private Methods 114 Exposing Private Objects 115 Returning Private Arrays 116 Deep Copy via JSON 117 Revealing Pattern 119 Constants 120 Namespaces 121 Modules 123 CommonJS Modules 124 Defining a CommonJS Module 124 Using a CommonJS Module 125 Using an Agnostic Module 126 AMD 127 Design Patterns 128 Singleton 128 Factory 130 Decorator 131 Documentation and Testing 133 Manual 133 Documenting Your Code 134 Unit Testing 134 JSLint 135 Linting This Book 135 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 viii | Table of Contents www.it-ebooks.info . www.it-ebooks.info www.it-ebooks.info Stoyan Stefanov JavaScript for PHP Developers www.it-ebooks.info JavaScript for PHP Developers by Stoyan Stefanov Copyright ©. scriptable with JavaScript. Fluency in JavaScript is a must for every web developer today. This book is for the PHP developer who’s learning JavaScript from