Javascript: the Good Parts by Douglas Crockford
Total Page:16
File Type:pdf, Size:1020Kb
1 JavaScript: The Good Parts by Douglas Crockford Publisher: O'Reilly Pub Date: May 2, 2008 Print ISBN-13: 978-0-596-51774-8 Pages: 170 Table of Contents | Index Overview Most programming languages contain good and bad parts, but JavaScript has more than its share of the bad, having been developed and released in a hurry before it could be refined. This authoritative book scrapes away these bad features to reveal a subset of JavaScript that's more reliable, readable, and maintainable than the language as a whole-a subset you can use to create truly extensible and efficient code. Considered the JavaScript expert by many people in the development community, author Douglas Crockford identifies the abundance of good ideas that make JavaScript an outstanding object-oriented programming language-ideas such as functions, loose typing, dynamic objects, and an expressive object literal notation. Unfortunately, these good ideas are mixed in with bad and downright awful ideas, like a programming model based on global variables. When Java applets failed, JavaScript became the language of the Web by default, making its popularity almost completely independent of its qualities as a programming language. In JavaScript: The Good Parts, Crockford finally digs through the steaming pile of good intentions and blunders to give you a detailed look at all the genuinely elegant parts of JavaScript, including: • Syntax • Objects • Functions • Inheritance • Arrays • Regular expressions • Methods • Style • Beautiful features The real beauty? As you move ahead with the subset of JavaScript that this book presents, you'll also sidestep the need to unlearn all the bad parts. Of course, if you want to find out more about the bad parts and how to use them badly, simply consult any other JavaScript book. With JavaScript: The Good Parts, you'll discover a beautiful, elegant, lightweight and highly expressive language that lets you create effective code, whether you're managing object libraries or just trying to get Ajax to run fast. If you develop sites or applications for the Web, this book is an absolute must. 1 2 2 1 JavaScript: The Good Parts by Douglas Crockford Publisher: O'Reilly Pub Date: May 2, 2008 Print ISBN-13: 978-0-596-51774-8 Pages: 170 Table of Contents | Index Dedication Preface Chapter 1. Good Parts Section 1.1. Why JavaScript? Section 1.2. Analyzing JavaScript Section 1.3. A Simple Testing Ground Chapter 2. Grammar Section 2.1. Whitespace Section 2.2. Names Section 2.3. Numbers Section 2.4. Strings Section 2.5. Statements Section 2.6. Expressions Section 2.7. Literals Section 2.8. Functions Chapter 3. Objects Section 3.1. Object Literals Section 3.2. Retrieval Section 3.3. Update Section 3.4. Reference Section 3.5. Prototype Section 3.6. Reflection Section 3.7. Enumeration Section 3.8. Delete Section 3.9. Global Abatement Chapter 4. Functions Section 4.1. Function Objects Section 4.2. Function Literal Section 4.3. Invocation Section 4.4. Arguments Section 4.5. Return Section 4.6. Exceptions Section 4.7. Augmenting Types Section 4.8. Recursion Section 4.9. Scope Section 4.10. Closure Section 4.11. Callbacks Section 4.12. Module Section 4.13. Cascade Section 4.14. Curry Section 4.15. Memoization Chapter 5. Inheritance Section 5.1. Pseudoclassical Section 5.2. Object Specifiers Section 5.3. Prototypal 1 2 Section 5.4. Functional Section 5.5. Parts Chapter 6. Arrays Section 6.1. Array Literals Section 6.2. Length Section 6.3. Delete Section 6.4. Enumeration Section 6.5. Confusion Section 6.6. Methods Section 6.7. Dimensions Chapter 7. Regular Expressions Section 7.1. An Example Section 7.2. Construction Section 7.3. Elements Chapter 8. Methods Chapter 9. Style Chapter 10. Beautiful Features Appendix A. Awful Parts Section A.1. Global Variables Section A.2. Scope Section A.3. Semicolon Insertion Section A.4. Reserved Words Section A.5. Unicode Section A.6. typeof Section A.7. parseInt Section A.8. + Section A.9. Floating Point Section A.10. NaN Section A.11. Phony Arrays Section A.12. Falsy Values Section A.13. hasOwnProperty Section A.14. Object Appendix B. Bad Parts Section B.1. == Section B.2. with Statement Section B.3. eval Section B.4. continue Statement Section B.5. switch Fall Through Section B.6. Block-less Statements Section B.7. ++ -- Section B.8. Bitwise Operators Section B.9. The function Statement Versus the function Expression Section B.10. Typed Wrappers Section B.11. new Section B.12. void Appendix C. JSLint Section C.1. Undefined Variables and Functions Section C.2. Members Section C.3. Options Section C.4. Semicolon Section C.5. Line Breaking Section C.6. Comma Section C.7. Required Blocks Section C.8. Forbidden Blocks 2 3 Section C.9. Expression Statements Section C.10. for in Statement Section C.11. switch Statement Section C.12. var Statement Section C.13. with Statement Section C.14. = Section C.15. == and != Section C.16. Labels Section C.17. Unreachable Code Section C.18. Confusing Pluses and Minuses Section C.19. ++ and -- Section C.20. Bitwise Operators Section C.21. eval Is Evil Section C.22. void Section C.23. Regular Expressions Section C.24. Constructors and new Section C.25. Not Looked For Section C.26. HTML Section C.27. JSON Section C.28. Report Appendix D. Syntax Diagrams Appendix E. JSON Section E.1. JSON Syntax Section E.2. Using JSON Securely Section E.3. A JSON Parser Colophon Index 3 4 4 1 JavaScript: The Good Parts by Douglas Crockford Copyright © 2008 Yahoo! Inc. 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 (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Editor: Simon St.Laurent Production Editor: Sumita Mukherji Copyeditor: Genevieve d'Entremont Proofreader: Sumita Mukherji Indexer: Julie Hawks Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Printing History: May 2008: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc. JavaScript: The Good Parts, the image of a Plain Tiger butterfly, and related trade dress are trademarks of O'Reilly Media, Inc. Java ⢠is a trademark of Sun Microsystems, 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 trademark 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. This book uses RepKoverâ¢, a durable and flexible lay-flat binding. ISBN: 978-0-596-51774-8 [M] 1 2 2 1 Dedication For the Lads: Clement, Philbert, Seymore, Stern, and, lest we forget, C. Twildo. 1 2 2 1 Preface If we offend, it is with our good will That you should think, we come not to offend, But with good will. To show our simple skill, That is the true beginning of our end. âWilliam Shakespeare, A Midsummer Night's Dream This is a book about the JavaScript programming language. It is intended for programmers who, by happenstance or curiosity, are venturing into JavaScript for the first time. It is also intended for programmers who have been working with JavaScript at a novice level and are now ready for a more sophisticated relationship with the language. JavaScript is a surprisingly powerful language. Its unconventionality presents some challenges, but being a small language, it is easily mastered. My goal here is to help you to learn to think in JavaScript. I will show you the components of the language and start you on the process of discovering the ways those components can be put together. This is not a reference book. It is not exhaustive about the language and its quirks. It doesn't contain everything you'll ever need to know. That stuff you can easily find online. Instead, this book just contains the things that are really important. This is not a book for beginners. Someday I hope to write a JavaScript: The First Parts book, but this is not that book. This is not a book about Ajax or web programming. The focus is exclusively on JavaScript, which is just one of the languages the web developer must master. This is not a book for dummies. This book is small, but it is dense. There is a lot of material packed into it. Don't be discouraged if it takes multiple readings to get it. Your efforts will be rewarded. P2.1. Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, filenames, and file extensions. Constant width Indicates computer coding in a broad sense. This includes commands, options, variables, attributes, keys, requests, functions, methods, types, classes, modules, properties, parameters, values, objects, events, event handlers, XML and XHTML tags, macros, and keywords. Constant width bold Indicates commands or other text that should be typed literally by the user. P2.2. Using Code Examples This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation.