Secrets of the JavaScript Ninja John Resig and Bear Bibeault Copyright For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: [email protected] ©2013 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editors: Jeff Bleiel, Sebastian Stirling 20 Baldwin Road Technical editor: Valentin Crettaz PO Box 261 Copyeditor: Andy Carroll Shelter Island, NY 11964 Proofreader: Melody Dolab Typesetter: Dennis Dalinnik Cover designer: Leslie Haimes Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 18 17 16 15 14 13 12 Brief Table of Contents Copyright Brief Table of Contents Table of Contents Preface Acknowledgments About this Book About the Authors 1. Preparing for training Chapter 1. Enter the ninja Chapter 2. Arming with testing and debugging 2. Apprentice training Chapter 3. Functions are fundamental Chapter 4. Wielding functions Chapter 5. Closing in on closures Chapter 6. Object-orientation with prototypes Chapter 7. Wrangling regular expressions Chapter 8. Taming threads and timers 3. Ninja training Chapter 9. Ninja alchemy: runtime code evaluation Chapter 10. With statements Chapter 11. Developing cross-browser strategies Chapter 12. Cutting through attributes, properties, and CSS 4. Master training Chapter 13. Surviving events Chapter 14. Manipulating the DOM Chapter 15. CSS selector engines Index List of Figures List of Tables List of Listings Table of Contents Copyright Brief Table of Contents Table of Contents Preface Acknowledgments About this Book About the Authors 1. Preparing for training Chapter 1. Enter the ninja 1.1. The JavaScript libraries we’ll be tapping 1.2. Understanding the JavaScript language 1.3. Cross-browser considerations 1.4. Current best practices 1.4.1. Current best practice: testing 1.4.2. Current best practice: performance analysis 1.5. Summary Chapter 2. Arming with testing and debugging 2.1. Debugging code 2.1.1. Logging 2.1.2. Breakpoints 2.2. Test generation 2.3. Testing frameworks 2.3.1. QUnit 2.3.2. YUI Test 2.3.3. JsUnit 2.3.4. Newer unit-testing frameworks 2.4. The fundamentals of a test suite 2.4.1. The assertion 2.4.2. Test groups 2.4.3. Asynchronous testing 2.5. Summary 2. Apprentice training Chapter 3. Functions are fundamental 3.1. What’s with the functional difference? 3.1.1. Why is JavaScript’s functional nature important? 3.1.2. Sorting with a comparator 3.2. Declarations 3.2.1. Scoping and functions 3.3. Invocations 3.3.1. From arguments to function parameters 3.3.2. Invocation as a function 3.3.3. Invocation as a method 3.3.4. Invocation as a constructor 3.3.5. Invocation with the apply() and call() methods 3.4. Summary Chapter 4. Wielding functions 4.1. Anonymous functions 4.2. Recursion 4.2.1. Recursion in named functions 4.2.2. Recursion with methods 4.2.3. The pilfered reference problem 4.2.4. Inline named functions 4.2.5. The callee property 4.3. Fun with function as objects 4.3.1. Storing functions 4.3.2. Self-memoizing functions 4.3.3. Faking array methods 4.4. Variable-length argument lists 4.4.1. Using apply() to supply variable arguments 4.4.2. Function overloading 4.5. Checking for functions 4.6. Summary Chapter 5. Closing in on closures 5.1. How closures work 5.2. Putting closures to work 5.2.1. Private variables 5.2.2. Callbacks and timers 5.3. Binding function contexts 5.4. Partially applying functions 5.5. Overriding function behavior 5.5.1. Memoization 5.5.2. Function wrapping 5.6. Immediate functions 5.6.1. Temporary scope and private variables 5.6.2. Loops 5.6.3. Library wrapping 5.7. Summary Chapter 6. Object-orientation with prototypes 6.1. Instantiation and prototypes 6.1.1. Object instantiation 6.1.2. Object typing via constructors 6.1.3. Inheritance and the prototype chain 6.1.4. HTML DOM prototypes 6.2. The gotchas! 6.2.1. Extending Object 6.2.2. Extending Number 6.2.3. Subclassing native objects 6.2.4. Instantiation issues 6.3. Writing class-like code 6.3.1. Checking for function serializability 6.3.2. Initialization of subclasses 6.3.3. Preserving super-methods 6.4. Summary Chapter 7. Wrangling regular expressions 7.1. Why regular expressions rock 7.2. A regular expression refresher 7.2.1. Regular expressions explained 7.2.2. Terms and operators
Description: