Tải bản đầy đủ (.pdf) (10 trang)

Phát triển Javascript - part 1 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.36 MB, 10 trang )

ptg
Download from www.eBookTM.com
ptg
Test-Driven JavaScript
Development
Download from www.eBookTM.com
ptg
Test-Driven JavaScript
Development
Christian Johansen
Upper Saddle River, NJ • Boston • Indianapolis • San Francisco
New York • Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City
Download from www.eBookTM.com
ptg
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 the publisher was
aware of a trademark claim, the designations have been printed with initial capital letters or
in all capitals.
The author and publisher have taken care in the preparation of this book, but make no
expressed or implied warranty of any kind and assume no responsibility for errors or
omissions. No liability is assumed for incidental or consequential damages in connection with
or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk
purchases or special sales, which may include electronic versions and/or custom covers and
content particular to your business, training goals, marketing focus, and branding interests.
For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419

For sales outside the United States please contact:


International Sales

Visit us on the Web: informit.com/aw
Library of Congress Cataloging-in-Publication Data
Johansen, Christian, 1982-
Test-driven JavaScript development / Christian Johansen.
p. cm.
Includes bibliographical references and index.
ISBN-13: 978-0-321-68391-5 (pbk. : alk. paper)
ISBN-10: 0-321-68391-9 (pbk. : alk. paper)
1. JavaScript (Computer program language) I. Title.
QA76.73.J39J64 2011
005.13’3–dc22 2010027298
Copyright
c
 2011 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by
copyright, and permission must be obtained from the publisher prior to any prohibited
reproduction, storage in a retrieval system, or transmission in any form or by any means,
electronic, mechanical, photocopying, recording, or likewise. For information regarding
permissions, write to:
Pearson Education, Inc.
Rights and Contracts Department
501 Boylston Street, Suite 900
Boston, MA 02116
Fax: (617) 671-3447
ISBN-13: 978-0-321-68391-5
ISBN-10: 0-321-68391-9
Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville,
Indiana.

First printing, September 2010
Acquisitions Editor
Trina MacDonald
Development Editor
Songlin Qiu
Managing Editor
John Fuller
Project Editor
Madhu Bhardwaj,
Glyph International
Project Coordinator
Elizabeth Ryan
Copy Editor
Mike Read
Indexer
Robert Swanson
Proofreader
David Daniels
Technical Reviewers
Andrea Giammarchi
Joshua Gross
Jacob Seidelin
Cover Designer
Gary Adair
Compositor
Glyph International
From the Library of WoweBook.Com
Download from www.eBookTM.com
ptg
To Frøydis and Kristin, my special ladies.

From the Library of WoweBook.Com
Download from www.eBookTM.com
ptg
This page intentionally left blank
From the Library of WoweBook.Com
Download from www.eBookTM.com
ptg
Contents
Preface xix
Acknowledgments xxv
About the Author xxvii
Part I Test-Driven Development 1
1. Automated Testing 3
1.1 The Unit Test 4
1.1.1 Unit Testing Frameworks 5
1.1.2 strftime for JavaScript Dates 5
1.2 Assertions 9
1.2.1 Red and Green 10
1.3 Test Functions, Cases, and Suites 11
1.3.1 Setup and Teardown 13
1.4 Integration Tests 14
1.5 Benefits of Unit Tests 16
1.5.1 Regression Testing 16
1.5.2 Refactoring 17
1.5.3 Cross-Browser Testing 17
1.5.4 Other Benefits 17
1.6 Pitfalls of Unit Testing 18
1.7 Summary 18
2. The Test-Driven Development Process 21
2.1 Goal and Purpose of Test-Driven Development 21

2.1.1 Turning Development Upside-Down 22
2.1.2 Design in Test-Driven Development 22
vii
From the Library of WoweBook.Com
Download from www.eBookTM.com
ptg
viii
Contents
2.2 The Process 23
2.2.1 Step 1: Write a Test 24
2.2.2 Step 2: Watch the Test Fail 25
2.2.3 Step 3: Make the Test Pass 26
2.2.3.1 You Ain’t Gonna Need It 26
2.2.3.2 Passing the Test for String.prototype.trim 27
2.2.3.3 The Simplest Solution that Could Possibly Work 27
2.2.4 Step 4: Refactor to Remove Duplication 28
2.2.5 Lather, Rinse, Repeat 29
2.3 Facilitating Test-Driven Development 29
2.4 Benefits of Test-Driven Development 30
2.4.1 Code that Works 30
2.4.2 Honoring the Single Responsibility Principle 30
2.4.3 Forcing Conscious Development 31
2.4.4 Productivity Boost 31
2.5 Summary 31
3. Tools of the Trade 33
3.1 xUnit Test Frameworks 33
3.1.1 Behavior-Driven Development 34
3.1.2 Continuous Integration 34
3.1.3 Asynchronous Tests 35
3.1.4 Features of xUnit Test Frameworks 35

3.1.4.1 The Test Runner 35
3.1.5 Assertions 36
3.1.6 Dependencies 37
3.2 In-Browser Test Frameworks 37
3.2.1 YUI Test 38
3.2.1.1 Setup 38
3.2.1.2 Running Tests 40
3.2.2 Other In-Browser Testing Frameworks 40
3.3 Headless Testing Frameworks 41
3.3.1 Crosscheck 42
3.3.2 Rhino and env.js 42
3.3.3 The Issue with Headless Test Runners 42
3.4 One Test Runner to Rule Them All 42
3.4.1 How JsTestDriver Works 43
3.4.2 JsTestDriver Disadvantages 44
3.4.3 Setup 44
3.4.3.1 Download the Jar File 44
3.4.3.2 Windows Users 45
3.4.3.3 Start the Server 45
3.4.3.4 Capturing Browsers 46
From the Library of WoweBook.Com
Download from www.eBookTM.com
ptg
Contents
ix
3.4.3.5 Running Tests 46
3.4.3.6 JsTestDriver and TDD 48
3.4.4 Using JsTestDriver From an IDE 49
3.4.4.1 Installing JsTestDriver in Eclipse 49
3.4.4.2 Running JsTestDriver in Eclipse 50

3.4.5 Improved Command Line Productivity 51
3.4.6 Assertions 51
3.5 Summary 52
4. Test to Learn 55
4.1 Exploring JavaScript with Unit Tests 55
4.1.1 Pitfalls of Programming by Observation 58
4.1.2 The Sweet Spot for Learning Tests 59
4.1.2.1 Capturing Wisdom Found in the Wild 59
4.1.2.2 Exploring Weird Behavior 59
4.1.2.3 Exploring New Browsers 59
4.1.2.4 Exploring Frameworks 60
4.2 Performance Tests 60
4.2.1 Benchmarks and Relative Performance 60
4.2.2 Profiling and Locating Bottlenecks 68
4.3 Summary 69
Part II JavaScript for Programmers 71
5. Functions 73
5.1 Defining Functions 73
5.1.1 Function Declaration 73
5.1.2 Function Expression 74
5.1.3 The Function Constructor 75
5.2 Calling Functions 77
5.2.1 The arguments Object 77
5.2.2 Formal Parameters and arguments 79
5.3 Scope and Execution Context 80
5.3.1 Execution Contexts 81
5.3.2 The Variable Object 81
5.3.3 The Activation Object 82
5.3.4 The Global Object 82
5.3.5 The Scope Chain 83

5.3.6 Function Expressions Revisited 84
5.4 The this Keyword 87
5.4.1 Implicitly Setting this 88
5.4.2 Explicitly Setting this 89
5.4.3 Using Primitives As this 89
5.5 Summary 91
From the Library of WoweBook.Com
Download from www.eBookTM.com
ptg
x
Contents
6. Applied Functions and Closures 93
6.1 Binding Functions 93
6.1.1 Losing this: A Lightbox Example 93
6.1.2 Fixing this via an Anonymous Function 95
6.1.3 Function.prototype.bind 95
6.1.4 Binding with Arguments 97
6.1.5 Currying 99
6.2 Immediately Called Anonymous Functions 101
6.2.1 Ad Hoc Scopes 101
6.2.1.1 Avoiding the Global Scope 101
6.2.1.2 Simulating Block Scope 102
6.2.2 Namespaces 103
6.2.2.1 Implementing Namespaces 104
6.2.2.2 Importing Namespaces 106
6.3 Stateful Functions 107
6.3.1 Generating Unique Ids 107
6.3.2 Iterators 109
6.4 Memoization 112
6.5 Summary 115

7. Objects and Prototypal Inheritance 117
7.1 Objects and Properties 117
7.1.1 Property Access 118
7.1.2 The Prototype Chain 119
7.1.3 Extending Objects through the Prototype Chain 121
7.1.4 Enumerable Properties 122
7.1.4.1 Object.prototype.hasOwnProperty 124
7.1.5 Property Attributes 126
7.1.5.1 ReadOnly 126
7.1.5.2 DontDelete 126
7.1.5.3 DontEnum 126
7.2 Creating Objects with Constructors 130
7.2.1 prototype and [[Prototype]] 130
7.2.2 Creating Objects with new 131
7.2.3 Constructor Prototypes 132
7.2.3.1 Adding Properties to the Prototype 132
7.2.4 The Problem with Constructors 135
7.3 Pseudo-classical Inheritance 136
7.3.1 The Inherit Function 137
7.3.2 Accessing [[Prototype]] 138
7.3.3 Implementing super 139
7.3.3.1 The
_
super Method 140
From the Library of WoweBook.Com
Download from www.eBookTM.com

×