0% found this document useful (0 votes)
13 views

Instant Access to Pro Javascript Design Patterns 1st Edition Ross Harmes ebook Full Chapters

The document provides links to various eBooks available for download, including titles on JavaScript and design patterns. It features works by authors such as Ross Harmes and Addy Osmani, covering topics like object-oriented JavaScript and HTML5/CSS3 design patterns. Additionally, it includes copyright information and acknowledgments for the authors and contributors.

Uploaded by

garmawrucks1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Instant Access to Pro Javascript Design Patterns 1st Edition Ross Harmes ebook Full Chapters

The document provides links to various eBooks available for download, including titles on JavaScript and design patterns. It features works by authors such as Ross Harmes and Addy Osmani, covering topics like object-oriented JavaScript and HTML5/CSS3 design patterns. Additionally, it includes copyright information and acknowledgments for the authors and contributors.

Uploaded by

garmawrucks1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Download the Full Version of the Ebook with Added Features ebookname.

com

Pro Javascript Design Patterns 1st Edition Ross


Harmes

https://ebookname.com/product/pro-javascript-design-
patterns-1st-edition-ross-harmes/

OR CLICK HERE

DOWLOAD NOW

Download more ebook instantly today at https://ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Learning JavaScript Design Patterns Addy Osmani

https://ebookname.com/product/learning-javascript-design-patterns-
addy-osmani/

ebookname.com

Pro HTML5 and CSS3 Design Patterns 1st Edition Michael


Bowers

https://ebookname.com/product/pro-html5-and-css3-design-patterns-1st-
edition-michael-bowers/

ebookname.com

Learning JavaScript Design Patterns A JavaScript and React


Developer s Guide 2nd Edition Adnan Osmani

https://ebookname.com/product/learning-javascript-design-patterns-a-
javascript-and-react-developer-s-guide-2nd-edition-adnan-osmani/

ebookname.com

Creativity in English as a Lingua Franca Idiom and


Metaphor 1st Edition Marie-Luise Pitzl

https://ebookname.com/product/creativity-in-english-as-a-lingua-
franca-idiom-and-metaphor-1st-edition-marie-luise-pitzl/

ebookname.com
The Effects of Early Social Emotional and Relationship
Experience on the Development of Young Orphanage Children
Monographs of the Society for Research in Child
Development 1st Edition The St. Petersburg-Usa Orphanage
https://ebookname.com/product/the-effects-of-early-social-emotional-
Research Team
and-relationship-experience-on-the-development-of-young-orphanage-
children-monographs-of-the-society-for-research-in-child-
development-1st-edition-the-st-petersb/
ebookname.com

Lacey Wells and Quick Reconstructing Criminal Law Text and


Materials Law in Context 4th Edition Celia Wells

https://ebookname.com/product/lacey-wells-and-quick-reconstructing-
criminal-law-text-and-materials-law-in-context-4th-edition-celia-
wells/
ebookname.com

Coping With Institutional Order Flow Zicklin School of


Business Financial Markets Series 1st Edition Robert A.
Schwartz
https://ebookname.com/product/coping-with-institutional-order-flow-
zicklin-school-of-business-financial-markets-series-1st-edition-
robert-a-schwartz/
ebookname.com

Language from the Body Iconicity and Metaphor in American


Sign Language 1st Edition Sarah F. Taub

https://ebookname.com/product/language-from-the-body-iconicity-and-
metaphor-in-american-sign-language-1st-edition-sarah-f-taub/

ebookname.com

Modern Capital Theory First Edition Dewey

https://ebookname.com/product/modern-capital-theory-first-edition-
dewey/

ebookname.com
Northern Cheyenne Ledger Art by Fort Robinson Breakout
Survivors Denise Low

https://ebookname.com/product/northern-cheyenne-ledger-art-by-fort-
robinson-breakout-survivors-denise-low/

ebookname.com

Pro JavaScript
Design Patterns

Ross Harmes and Dustin Diaz


Pro JavaScript™ Design Patterns
Copyright © 2008 by Ross Harmes and Dustin Diaz
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-59059-908-2
ISBN-10 (pbk): 1-59059-908-X
ISBN-13 (electronic): 978-1-4302-0495-4
ISBN-10 (electronic): 1-4302-0495-8
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems Inc. in the
United States and other countries. Apress Inc. is not affiliated with Sun Microsystems Inc., and this book
was written without endorsement from Sun Microsystems Inc.
Lead Editors: Chris Mills, Tom Welsh
Technical Reviewer: Simon Willison
Editorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick,
Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper,
Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Richard Dal Porto
Copy Editor: Jennifer Whipple
Associate Production Director: Kari Brooks-Copony
Production Editor: Kelly Winquist
Compositor and Artist: Kinetic Publishing Services, LLC
Proofreader: Dan Shaw
Indexer: Julie Grady
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or
visit http://www.springeronline.com.
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley,
CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com.
To Mom, and those who have listened, thanks
—Dustin Diaz

To Alec, Dymphi, and Terry


—Ross Harmes
Contents at a Glance

About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv


About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

PART 1 ■■■ Object-Oriented JavaScript


■CHAPTER 1 Expressive JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
■CHAPTER 2 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
■CHAPTER 3 Encapsulation and Information Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . 25
■CHAPTER 4 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
■CHAPTER 5 The Singleton Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
■CHAPTER 6 Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

PART 2 ■■■ Design Patterns


■CHAPTER 7 The Factory Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
■CHAPTER 8 The Bridge Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
■CHAPTER 9 The Composite Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
■CHAPTER 10 The Facade Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
■CHAPTER 11 The Adapter Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
■CHAPTER 12 The Decorator Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
■CHAPTER 13 The Flyweight Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
■CHAPTER 14 The Proxy Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
■CHAPTER 15 The Observer Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
■CHAPTER 16 The Command Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
■CHAPTER 17 The Chain of Responsibility Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

v
Contents

About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv


About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

PART 1 ■■■ Object-Oriented JavaScript


■CHAPTER 1 Expressive JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The Flexibility of JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
A Loosely Typed Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Functions As First-Class Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The Mutability of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Design Patterns in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

■CHAPTER 2 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
What Is an Interface? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Benefits of Using Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Drawbacks of Using Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
How Other Object-Oriented Languages Handle Interfaces. . . . . . . . . . . . . 12
Emulating an Interface in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Describing Interfaces with Comments . . . . . . . . . . . . . . . . . . . . . . . . . 14
Emulating Interfaces with Attribute Checking . . . . . . . . . . . . . . . . . . . 16
Emulating Interfaces with Duck Typing . . . . . . . . . . . . . . . . . . . . . . . . 17
The Interface Implementation for This Book . . . . . . . . . . . . . . . . . . . . . . . . . 18
The Interface Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
When to Use the Interface Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
How to Use the Interface Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Example: Using the Interface Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Patterns That Rely on the Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
vii
viii ■CONTENTS

■CHAPTER 3 Encapsulation and Information Hiding . . . . . . . . . . . . . . . . . . . . 25


The Information Hiding Principle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Encapsulation vs. Information Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . 26
The Role of the Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Basic Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Fully Exposed Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Private Methods Using a Naming Convention . . . . . . . . . . . . . . . . . . . 30
Scope, Nested Functions, and Closures . . . . . . . . . . . . . . . . . . . . . . . . 32
Private Members Through Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
More Advanced Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Static Methods and Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Singletons and Object Factories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Benefits of Using Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Drawbacks to Using Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

■CHAPTER 4 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Why Do You Need Inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Classical Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
The Prototype Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
The extend Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Prototypal Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Asymmetrical Reading and Writing of Inherited Members . . . . . . . . 46
The clone Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Comparing Classical and Prototypal Inheritance . . . . . . . . . . . . . . . . . . . . . 49
Inheritance and Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Mixin Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Example: Edit-in-Place . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Using Classical Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Using Prototypal Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Using Mixin Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
When Should Inheritance Be Used? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

■CHAPTER 5 The Singleton Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65


The Basic Structure of the Singleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Namespacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
■CONTENTS ix

A Singleton As a Wrapper for Page-Specific Code . . . . . . . . . . . . . . . . . . . . 68


A Singleton with Private Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Using the Underscore Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Using Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Comparing the Two Techniques. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Lazy Instantiation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Example: Creating XHR Objects with Branching . . . . . . . . . . . . . . . . . . . . . 79
When Should the Singleton Pattern Be Used? . . . . . . . . . . . . . . . . . . . . . . . 81
Benefits of the Singleton Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Drawbacks of the Singleton Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

■CHAPTER 6 Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
The Structure of a Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Building a Chainable JavaScript Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Using Callbacks to Retrieve Data from Chained Methods . . . . . . . . . . . . . 89
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

PART 2 ■■■ Design Patterns


■CHAPTER 7 The Factory Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
The Simple Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
The Factory Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
When Should the Factory Pattern Be Used? . . . . . . . . . . . . . . . . . . . . . . . . . 99
Dynamic Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Combining Setup Costs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Abstracting Many Small Objects into One Large Object . . . . . . . . . . 99
Example: XHR Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Specialized Connection Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Choosing Connection Objects at Run-Time . . . . . . . . . . . . . . . . . . . . 103
Example: RSS Reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Benefits of the Factory Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Drawbacks of the Factory Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
x ■CONTENTS

■CHAPTER 8 The Bridge Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109


Example: Event Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Other Examples of Bridges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Bridging Multiple Classes Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Example: Building an XHR Connection Queue . . . . . . . . . . . . . . . . . . . . . . 111
Including the Core Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Including an Observer System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Developing the Queue Skeleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Implementing the Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Where Have Bridges Been Used? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
When Should the Bridge Pattern Be Used? . . . . . . . . . . . . . . . . . . . . . . . . . 122
Benefits of the Bridge Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Drawbacks of the Bridge Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

■CHAPTER 9 The Composite Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

The Structure of the Composite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126


Using the Composite Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Example: Form Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Adding Operations to FormItem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Adding Classes to the Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Adding More Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Example: Image Gallery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Benefits of the Composite Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Drawbacks of the Composite Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

■CHAPTER 10 The Facade Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141


Some Facade Functions You Probably Already Know About. . . . . . . . . . . 141
JavaScript Libraries As Facades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Facades As Convenient Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Example: Setting Styles on HTML Elements . . . . . . . . . . . . . . . . . . . . . . . . 144
Example: Creating an Event Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
General Steps for Implementing the Facade Pattern . . . . . . . . . . . . . . . . . 147
When Should the Facade Pattern Be Used? . . . . . . . . . . . . . . . . . . . . . . . . 148
Benefits of the Facade Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Drawbacks of the Facade Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
■CONTENTS xi

■CHAPTER 11 The Adapter Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149


Characteristics of an Adapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Adapting Existing Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Example: Adapting One Library to Another . . . . . . . . . . . . . . . . . . . . . . . . . 150
Example: Adapting an Email API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Wrapping the Webmail API in an Adapter . . . . . . . . . . . . . . . . . . . . . 157
Migrating from fooMail to dedMail . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
When Should the Adapter Pattern Be Used? . . . . . . . . . . . . . . . . . . . . . . . . 158
Benefits of the Adapter Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Drawbacks of the Adapter Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

■CHAPTER 12 The Decorator Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159


The Structure of the Decorator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
The Role of the Interface in the Decorator Pattern . . . . . . . . . . . . . . 163
The Decorator Pattern vs. the Composite Pattern . . . . . . . . . . . . . . 163
In What Ways Can a Decorator Modify Its Component? . . . . . . . . . . . . . . 164
Adding Behavior After a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Adding Behavior Before a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Replacing a Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Adding New Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
The Role of the Factory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Function Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
When Should the Decorator Pattern Be Used? . . . . . . . . . . . . . . . . . . . . . . 173
Example: Method Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Benefits of the Decorator Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Drawbacks of the Decorator Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

■CHAPTER 13 The Flyweight Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179


The Structure of the Flyweight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Example: Car Registrations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Intrinsic and Extrinsic State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Instantiation Using a Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Extrinsic State Encapsulated in a Manager . . . . . . . . . . . . . . . . . . . . 182
Managing Extrinsic State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Example: Web Calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Converting the Day Objects to Flyweights . . . . . . . . . . . . . . . . . . . . . 185
Where Do You Store the Extrinsic Data? . . . . . . . . . . . . . . . . . . . . . . 186
xii ■CONTENTS

Example: Tooltip Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186


The Unoptimized Tooltip Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Tooltip As a Flyweight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Storing Instances for Later Reuse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
When Should the Flyweight Pattern Be Used? . . . . . . . . . . . . . . . . . . . . . . 192
General Steps for Implementing the Flyweight Pattern . . . . . . . . . . . . . . 193
Benefits of the Flyweight Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Drawbacks of the Flyweight Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

■CHAPTER 14 The Proxy Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197


The Structure of the Proxy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
How Does the Proxy Control Access to Its Real Subject? . . . . . . . 197
Virtual Proxy, Remote Proxy, and Protection Proxy . . . . . . . . . . . . . . 200
The Proxy Pattern vs. the Decorator Pattern . . . . . . . . . . . . . . . . . . . 201
When Should the Proxy Be Used? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Example: Page Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
General Pattern for Wrapping a Web Service . . . . . . . . . . . . . . . . . . . . . . . 205
Example: Directory Lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
General Pattern for Creating a Virtual Proxy . . . . . . . . . . . . . . . . . . . . . . . . 210
Benefits of the Proxy Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Drawbacks of the Proxy Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214

■CHAPTER 15 The Observer Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215


Example: Newspaper Delivery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Push vs. Pull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Pattern in Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Building an Observer API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Delivery Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Subscribe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Unsubscribe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Observers in Real Life. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Example: Animation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Event Listeners Are Also Observers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
When Should the Observer Pattern Be Used? . . . . . . . . . . . . . . . . . . . . . . 223
Benefits of the Observer Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Drawbacks of the Observer Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
■CONTENTS xiii

■CHAPTER 16 The Command Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225


The Structure of the Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Creating Commands with Closures . . . . . . . . . . . . . . . . . . . . . . . . . . 227
The Client, the Invoker, and the Receiver. . . . . . . . . . . . . . . . . . . . . . 227
Using Interfaces with the Command Pattern. . . . . . . . . . . . . . . . . . . 228
Types of Command Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Example: Menu Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
The Menu Composites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
The Command Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Adding More Menu Items Later On . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Example: Undo and Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Implementing Undo with Nonreversible Actions By Logging
Commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Logging Commands for Crash Recovery . . . . . . . . . . . . . . . . . . . . . . 242
When to Use the Command Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Benefits of the Command Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Drawbacks of the Command Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

■CHAPTER 17 The Chain of Responsibility Pattern . . . . . . . . . . . . . . . . . . . . . . 245


The Structure of the Chain of Responsibility. . . . . . . . . . . . . . . . . . . . . . . . 245
Passing on Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Implementing a Chain of Responsibility in an Existing Hierarchy . . . . . . 254
Event Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
When Should the Chain of Responsibility Pattern Be Used? . . . . . . . . . . 255
Example: Image Gallery Revisited. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Using the Chain of Responsibility to Make Composites
More Efficient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Adding Tags to Photos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Benefits of the Chain of Responsibility Pattern . . . . . . . . . . . . . . . . . . . . . 261
Drawbacks of the Chain of Responsibility Pattern . . . . . . . . . . . . . . . . . . . 262
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
About the Authors

■ROSS HARMES is a front-end engineer for Yahoo! in Sunnyvale, California.


Educated as an electrical and computer engineer, Ross quickly put down
the soldering iron and oscilloscope and focused on the software aspect of
his degree. After discovering that debugging memory leaks is not much fun,
he dove into the muddy and turbulent waters of web programming. He has
been happily swimming there ever since.
This is Ross’s first book, but he has been publishing his stray thoughts
online for years. These days his technical ramblings can be found at
http://techfoolery.com.

■DUSTIN DIAZ is a user interface engineer for Google in Mountain View,


California. He enjoys writing JavaScript, CSS, and HTML, as well as making
interactive and usable interfaces to inspire passionate users. Dustin has
written articles for Vitamin and Digital Web Magazine, and posts regularly
about web development at his site, http://dustindiaz.com.

xv
About the Technical Reviewer

■SIMON WILLISON is a consultant on client- and server-side web development and a cocreator
of the Django web framework. Simon’s interests include OpenID, unobtrusive JavaScript,
and rapid application development. Before going freelance, Simon worked on Yahoo!’s
Technology Development team, and prior to that at the Lawrence Journal-World, an award-
winning local newspaper in Kansas. Simon maintains a popular web development weblog
at http://simonwillison.net/.

xvii
Acknowledgments

T hanks to our intrepid technical reviewer, Simon Willison, without whom this book would be
much less accurate, practical, and interesting. He worked tirelessly to provide amazing feedback
for each and every chapter.
Thanks to our colleagues and coworkers who took the time to wade through the early drafts
and provide notes and corrections. Dave Marr and Ernest Delgado in particular went above and
beyond and were instrumental in finding typos, technical errors, and poorly worded sentences.
Also, thanks to Lindsey Simon and Robert Otani, each of whom supported us by providing
ceaseless JavaScript humor.
Thanks to our friends and family, who stood by patiently while we bored them to death
with our endless tales of writing and incomprehensible technical minutiae. Your support kept
us going.
And lastly, we both wish to give our sincerest thanks to the people at Apress who made
this book a reality. The patience, understanding, and perseverance of Chris Mills, Tom Welsh,
Dominic Shakeshaft, Richard Dal Porto, and Jennifer Whipple deserve special recognition and
won’t be forgotten.

xix
Introduction

J avaScript is at a turning point. The language and those who program with it have matured.
People are starting to realize that it is a complex subject, worthy of further study.
Design patterns have been used in programming for years. They were first formally docu-
mented in Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides
(affectionately known as the Gang of Four) and have been applied to countless object-oriented
languages. Part of the appeal of design patterns is that they can be used uniformly over many
different languages and syntaxes. The basic structure stays the same; only the details change.
It is fairly easy, for instance, to take a pattern implemented in Java and convert it to C++.
The same cannot be said of JavaScript. While all of the same capabilities exist, they are
often not official parts of the language and must be emulated through obscure tricks and
unintuitive techniques. Over the years, people have discovered ways of using the language to
accomplish tasks never imagined by its creators. We must do likewise to implement common
object-oriented features.
This book collects and documents those tricks and techniques. In the first part, we create
a base of object-oriented features that we can build upon to implement specific design patterns.
The second part deals with specific design patterns and how they can be used in the JavaScript
language.
We took great pains to make the examples in each chapter as practical as possible. We
tried to list some of the most common tasks performed by JavaScript programmers, and then
used design patterns to make them more modular, efficient, and easily maintained. When we
do venture into more theoretical examples, it is done to illustrate a specific point. We know
that at the end of the day, the value of this book will be judged by its relevance to your every-
day tasks and projects.
We hope you enjoy this book. JavaScript is an incredibly complex and flexible language,
and one that is well-suited to experimentation. Play around with any of our code examples.
Let us know if you find a novel way of implementing a pattern, or a new use for an old tech-
nique. More information and downloadable code examples can be found at the book’s
website, http://jsdesignpatterns.com, and at the Apress website, http://www.apress.com.

Who This Book Is For


This book is meant primarily for two types of people. The first is web developers or front-end
engineers who know some JavaScript and wish to learn more. Specifically, those who want to
improve their understanding of the object-oriented capabilities of JavaScript and learn how
they can make their code more modular, maintainable, and efficient. This book will teach
these readers about the basics of object-oriented programming in JavaScript. It will also teach
them about specific design patterns, showing when they can be used and how to implement

xxi
Random documents with unrelated
content Scribd suggests to you:
house.
And then, when it grew dark, Uncle Wiggily called together ten
thousand firefly-lightning bugs, and they flittered and fluttered about
the porch, on which the boy had been taken after supper. The fireflies
made pinwheels of themselves, they went up like skyrockets, they
leaped about in bunches like the balls from Roman candles and finally,
when it was time to go to bed, they took hold of each others' legs
and, clinging together, spelled out:

"Oh, it's just like real fireworks!" cried the happy boy.
"I'm glad he liked it!" said Uncle Wiggily, as he hopped home to his
hollow stump bungalow.
So if the pussy cat doesn't claw the tail off the letter Q and make it
look like a big, round O, I'll tell you next about Uncle Wiggily and the
little boy's skates.
STORY XIII
UNCLE WIGGILY AND THE SKATES

There was once a little boy to whom Santa Claus brought a pair of
skates at Christmas. And, of course, that boy, as soon as he saw the
shiny, steel runners, wished that the pond would freeze over so that he
might try his new playthings.
"When do you s'pose there'll be skating?" he asked his mother again
and again, for, as yet, there was only a "skim" of ice on the pond.
"Oh, pretty soon," his mother would answer. "You mustn't go skating
when the ice is too thin, you know. If you did you would break through,
into the cold water."
"And that would spoil my skates, wouldn't it?" asked the boy.
"Yes, but besides that you might be drowned, or catch cold and be very
ill," Mother said. "So keep off the ice with your new skates until the
pond has frozen good and thick."
"Yes'm, I will," promised the little boy, and, really, he meant to keep his
word. But as the days passed, and the weather was not quite cold
enough to freeze thick ice, the little boy became tired of waiting.
Every chance he had, after school, he would go down to the edge of the
pond, and throw stones on the ice to see how thick it was. Often the
stones would break through, and fall into the cold, black water with a
"thump!" Then the boy would know the ice was not thick enough.
"I don't want to fall through like a stone," he would say, and back to his
house he would go with his new skates dangling and jingling at his
back, over which they were hung by a strap.
But one day, when the boy threw a large stone on the ice of the pond,
instead of breaking through, the rock only made a dent and stayed
there.
"Oh, hurray!" cried the boy. "I guess it's strong enough to hold me now!
I'm going skating!"
However, first he started to walk on the edge of the ice near the shore,
and when he did so, and heard cracking sounds, he jumped quickly
back.
"I guess I'd better not try it yet," said the boy to himself. "I'll wait a little
while until it freezes harder."
So he sat down by the edge of the pond to wait for the ice to freeze
harder. But as he sat there, and saw how white and shiny it was, and as
he looked at his new skates, which he had only put on in the house, that
boy couldn't wait another minute.
He walked along the shore a little farther, to a place where the ice
seemed more hard and shiny and there, after throwing some stones,
and venturing out a little way, finding that there was no cracking sound,
the little boy made up his mind to try to skate. There was no one else
on the pond—no other boys and girls, and it was a bit lonesome. But
the boy was so eager to try his new skates that he did not think of this.
Down he sat on the ground, and began putting on his Christmas skates.
And it was just about this time that Nurse Jane Fuzzy Wuzzy, Uncle
Wiggily's muskrat lady housekeeper, happened to look out of the
window of the hollow stump bungalow. The bunny's bungalow was so
hidden in the woods, near the pond, that few boys or girls ever saw the
queer little house. But Uncle Wiggily could see them, as they came to
the woods winter and summer, and often he was able to help them.
"Well, I declare!" exclaimed Nurse Jane, as she looked out of the
window a second time.
"What's the matter?" asked Uncle Wiggily, who was just finishing his
breakfast of lettuce bread and carrot coffee, with some turnip
marmalade.
"Why, there's a boy—a real boy and not one of the animal chaps—
getting ready to go skating!" said the muskrat lady, for she could see
the boy putting on his skates.
"That ice isn't thick enough for real boys or girls to skate on," the bunny
gentleman said. "It would be all right for Sammie Littletail, or Johnnie or
Billie Bushytail, but real boys are too heavy—much heavier than my
nephew Sammie the rabbit, or than the bushytail squirrel chaps."
"Well, this boy is going on all the same," cried Nurse Jane. "And I know
he'll break through, and he'll frighten his mother into a conniption fit!"
"That will be too bad!" exclaimed Uncle Wiggily, as he wiped a little of
the turnip marmalade off his whiskers, where it had fallen by mistake. "I
must try to save him if he does fall in!"
"It would be better to keep him from going on the ice," spoke Nurse
Jane. "Safety first, you know!"
"If I could speak boy language I'd hop down there and tell him the ice is
too thin," answered Uncle Wiggily. "But though I know what the boys
and girls say, I cannot, myself, speak their talk. However, I think I know
a way to save this boy, if he happens to break through the ice."
"Well, he's almost sure to break through," declared Miss Fuzzy Wuzzy,
"so you'd better hurry."
"No sooner said than done!" exclaimed Uncle Wiggily, and, catching up
his red, white and blue striped rheumatism crutch, and putting on his fur
cap (for the day was cold), away the bunny hopped from his hollow
stump bungalow.
Instead of going to the place where the boy, with his skates fastened on
his shoes, was about to try the ice, the bunny gentleman went to the
house of some friends of his. The house would seem queer to you, for
all it looked like was a pile of sticks half buried in the frozen pond.
But in this house lived a family of beavers—queer animals whose fur is
so warm and thick that they can swim in ice water and not feel chilly. In
fact the beavers had to dive down under the ice and water to get into
their winter home.
"Are Toodle and Noodle in the house?" asked Uncle Wiggily, as he
reached the stick-house. On shore, not far from it, was Grandpa
Whackum, the old beaver gentleman, with his broad, flat tail.
"Why, yes, Toodle and Noodle are inside," answered Grandpa Whackum.
"Shall I call them out?"
"If you please," spoke Uncle Wiggily. "I want them to come and help me
save a boy who, I think, is going to break through the thin ice with his
new skates."
"That will be too bad!" exclaimed Grandpa Whackum. Then with his
broad tail he pounded or "whacked" on the ground, and soon up
through a hole in the ice came swimming Toodle and Noodle Flat-Tail,
the two beaver boys.

"Oh, hello, Uncle Wiggily!" they called. "We're glad to see you!"
"Hello!" answered the bunny gentleman. "Will you come with me, and
help save a real boy?"
"Of course," said Toodle, shaking off some ice water from his fur coat.
"He won't try to catch us, will he?" asked Noodle.
"I think not," the bunny gentleman replied. "If what I think is going to
happen, does really happen, that boy will be too surprised to catch
anything but a cold! Come along, beaver chaps!"
So Toodle and Noodle, wet and glistening from having dived out of their
house, and down under water to come up through the hole in the ice,
followed Uncle Wiggily. The sun and wind soon dried their fur.
"There's the boy," said Uncle Wiggily, as he and the beaver chaps
reached the edge of the pond. "He's skating on thin ice. He'll go through
in a minute!"
And, surely enough, hardly had the bunny spoken than there was a
cracking sound, the ice broke beneath the boy's feet and into the dark,
cold water he fell.
"Oh! Oh!" cried the boy. "Help me, somebody! Oh! Oh!"
"Ha! It's a good thing Nurse Jane saw him!" said Uncle Wiggily. "Quick
now, Toodle and Noodle! I brought you along because you have such
good, sharp teeth—much sharper and better than mine are for gnawing
down trees. I can gnaw off the bark, but you can nibble all the way
through a tree and make it fall."
"Is that what you want us to do?" asked Toodle.
"Yes," answered Uncle Wiggily. "We'll go close to shore, where the boy
has fallen in. Near him is a tree. You'll gnaw that so it will fall outward
across the ice, and he can reach up, take hold of it and pull himself out
of the hole."
By this time the poor boy was floundering around in the cold water. He
tried to get hold of the edges of the ice around the hole through which
he had fallen, but the ice broke in his hands.
"Help! Help!" he cried.
"We're going to help you," answered Uncle Wiggily, but, of course, he
spoke animal language which the boy did not understand. But Toodle
and Noodle understood, and quickly running to the edge of the shore
they gnawed and gnawed and gnawed very extra fast at an overhanging
tree until it began to bend and break. Uncle Wiggily gnawed a little,
also, to help the beaver boys.
Then, just as the real boy was almost ready to sink down under water,
the tree fell on the ice, some of its branches close enough so the boy
skater could grasp them.
"Oh, now I can pull myself out!" he said. "This tree fell just in time! Now
I'll be saved!"
He did not know that Uncle Wiggily and the beaver boys had gnawed
the tree down, making it fall just in the right place at the right time. For
the boy was so frightened at having broken through the ice, that he
never noticed the bunny gentleman and the beaver boys on shore.
He caught hold of the tree branches in his cold fingers, pulled himself up
out of the water, that boy did; and to shore. Then as he sat down, all
wet and shivering, to take off his skates, so he could run home, Uncle
Wiggily called to Toodle and Noodle:
"Come on, beaver boys! Our work is done! We have saved that boy, and
I hope he never again tries to skate on thin ice."
Then Uncle Wiggily hopped toward his hollow stump bungalow, and the
beaver boys slid on the ice, near shore, toward their own stick-house,
for the pond was frozen hard and thick enough to hold them. And the
boy ran home as fast as he could, and drank hot lemonade so he
wouldn't catch cold.
He did get the snuffles, but of course that couldn't be helped, and it
wasn't much for falling through the ice; was it?
"You never should have gone skating until the pond was better frozen,"
his mother said.
"I know it," the boy answered. "But wasn't it lucky that tree fell when it
did?"
"Very lucky!" agreed his mother. And neither the boy nor his mother
knew that it was Nurse Jane, Uncle Wiggily and the beaver boys who
had caused the tree to topple over just in time.
But that's the way it sometimes is in this world. And if the cow doesn't
tickle the man in the moon with her horns, when she jumps over the
green cheese, I'll tell you next about Uncle Wiggily going coasting.
STORY XIV
UNCLE WIGGILY GOES COASTING

"Oh, it's stopped snowing! It's stopped snowing! Now we can go


coasting; can't we, Mother?"
"And on our new Christmas sleds! Oh, what fun!"
A boy and a girl ran from the window, against which they had been
pressing their noses, looking out to see when the white flakes would
stop falling from the sky. Now the storm seemed to be over, leaving
the ground covered with the sparkling snow crystals.
"Yes, you may go coasting a little while," said Mother. "But don't stay
too late. When Daddy comes to supper you must be home."
"We will!" promised the boy and girl, and, laughing in glee, they ran
to get on their boots, their mittens and warm coats.
"I want to go coasting! Take me to slide down hill!" cried Bumps, the
little sister of the boy and girl. "I want a sleigh ride."
"Oh, Bumps, you're too little!" objected Sister.
"And she'll fall down and bang herself," added Brother. In fact the
"littlest girl" did fall down so often that she was called "Bumps" as a
pet name.
"I won't fall down!" Bumps promised. "I'll be good! Please take me
coasting?"
"I think you might take her," said Mother.
"Yes, we will," spoke Sister. "Come on, Bumps!"
"Well, if she falls off the sled when it's going down hill, and she gets
bumped, it won't be my fault!" declared Brother.
"I—I'll be good—I won't fall!" promised Bumps. So Mother bundled
her up, and out she went to the coasting hill with Brother and Sister,
each of whom had a sled.
"I'm not going to give her rides on my sled all the while!" said
Brother, half grumbling.
"We'll take turns," more kindly suggested Sister. "Take hold of my
hand, Bumps, and don't fall any more times than you can help,
dear!"
"No; I won't," answered Bumps. The littlest girl was smiling and
happy because she was going coasting with Sister and Brother. And
she made up her mind she would try very, very hard not to fall.
On the other side of the forest, near which was the coasting hill of
the children, lived Uncle Wiggily in his hollow stump bungalow. From
afar he had often watched the boys and girls sliding down on their
sleds, but the bunny gentleman had never gone very close.
"For," he said to himself, "they might, by accident, run over me. And,
though I haven't much of a tail to be cut off, I would look queer if
anything should happen to my long ears. I'll keep away from the
coasting hill of the boys and girls."
But not far from the bunny's bungalow was another and smaller hill,
down which the animal boys and girls coasted. Of course, very few
of them had such sleds as you children have, with shiny steel
runners, and with the tops painted red, blue, green and gold. In
fact, some of the animal boys didn't bother with a sled at all.
Take Toodle and Noodle Flat-Tail, the beaver chaps, for instance.
They just slid down hill on their broad, flat tails. And as for Johnnie
and Billie Bushytail, the squirrels, they sat on their fuzzy tails and
scooted down the hill of snow. Others of the animal children
sometimes used pieces of wood, an old board or some sticks bound
together with strands from a wild grape vine.
And about the time that Sister, Brother and Bumps went coasting,
Sammie and Susie Littletail, the rabbits, passed the hollow stump
bungalow of Uncle Wiggily Longears. The little bunnies were each
pulling a sled made from pieces of birch bark they had gnawed from
trees.
"Let's ask Uncle Wiggily to go coasting with us," spoke Susie.
"Oh, yes! Let's!" echoed Sammie. "It'll be lots of fun!"
And Uncle Wiggily was very glad to go coasting. Out of his bungalow
he hopped, his pink nose twinkling twice as fast as the shiny star on
top of the Christmas tree.
"Dear me, Wiggy!" cried Nurse Jane. "You don't mean to say you're
going coasting with your rheumatism!"
"No, I'm going coasting with Sammie and Susie," the laughing bunny
answered. "I haven't any rheumatism to go coasting with to-day, I'm
glad to tell you." And, surely enough, he didn't need to take his red,
white and blue striped crutch.
When Sammie, Susie and Uncle Wiggily reached the coasting hill,
they found there many of the animal children.
"Oh, Uncle Wiggily! Ride on my sled!" invited one after another.
"Ride on mine! Coast with me!"
"I'll take turns with each one!" promised the bunny gentleman, and
so he did, riding with Sammie and Susie first, then with the Bushytail
squirrel brothers, next with Lulu, Alice and Jimmie Wibblewobble,
the ducks, and so on down to Dottie and Willie Flufftail, the lamb
children.
Oh, such fun as Uncle Wiggily had on the animal children's coasting
hill. And on the other side of the forest, Sister, Brother and Bumps
had their fun, with the real boys and girls.
At last it began to grow dusk, and when Uncle Wiggily was thinking
of telling the animal children it was time for them to leave for home,
up came rushing Jackie and Peetie Bow Wow, the puppy dog boys.
"Oh, Uncle Wiggily!" barked Jackie. "We were just over to the big
hill, where the real boys coast, and we saw——"
"We saw a little baby girl—that is, almost a baby—in a pile of snow!"
finished Peetie, for his brother Jackie was out of breath and couldn't
bark any more.
"What's that?" cried Uncle Wiggily. "A real, live little girl in the
snow?"
"Right in a snow drift!" barked Jackie. "All alone!"
"Why," said the bunny gentleman, as he thought it over, "she must
have been coasting with her brother or sister, and maybe she fell off
a sled and went down deep in the snow. And they played so hard
they never missed her! But she mustn't be allowed to stay asleep in
the snow. She'll freeze!"
"If she's only a little one—almost a baby—couldn't we put her on
one of our sleds?" asked Sammie.
"And ride her home," went on Susie.
"If we all pull together we'd be strong enough to pull a real, live girl,
if she wasn't too large," quacked Jimmie Wibblewobble, the duck.
"We'll try!" said Uncle Wiggily. "All of you take the grape-vine ropes
from your sleds and follow me."
Quickly the animal children did this, taking with them only the large
double sled of Neddie Stubtail, the boy bear, which was the largest
sled of all. It was low and flat, and Uncle Wiggily thought it would be
easy to roll a little girl up on it and pull her along.
Soon Uncle Wiggily and the animal children reached the hill where
the real boys and girls had coasted. None of them was there now, all
having gone home to their suppers.
"Here she is!" softly barked Jackie, leading the way to a snowbank,
at the foot of the hill.
And there, sound asleep in the soft, warm snow was—Bumps!
Yes, as true as I'm telling you—Bumps!
The little girl had been sliding down with her sister, and had rolled
off the sled at the bottom of the hill after about the forty-'leventh
coast. And Bumps was so tired, and sleepy, from having been
outdoors so long, that, as soon as she rolled from the sled into the
snow, she fell asleep! Think of that!
And as Sister wanted to have a race with Brother and some of the
other children, she never noticed what happened to Bumps. But
there she was—in the snow asleep. Poor little Bumps!
"It will never do to leave her here!" whispered Uncle Wiggily to the
animal boys and girls. "Don't awaken her, but roll her over on
Neddie's sled, and we'll pull her to her home. I know where she
lives. We'll leave her in front of the door, I'll throw a snowball to
make a sound like a knock, and then we can run away. Her father
and mother will come out and take her in."
So all working together, pushing, pulling, tugging and rolling most
gently, the bunny gentleman and the animal boys and girls slid
Bumps upon the low sled of the bear boy. Then they fastened the
grape-vine ropes to it, and, all taking hold, off they started over the
snow toward the village.
It was almost dark, so no one saw the strange procession of Uncle
Wiggily and his friends; and the bunny gentleman was glad of this.
Right up to the home of Bumps they pulled her, and just as they got
the sled in her yard Bumps opened her eyes.
"Oh! Oh! Oh!" she cried when she saw all the animal children, and
Uncle Wiggily, too, standing around her. "I'm in fairyland! Oh, how I
love it!"
"Quick, Sammie—Susie—Jackie—Peetie—scoot away!" called Uncle
Wiggily in animal talk, and the rabbits, squirrels, guinea pigs, ducks,
bears, beavers and others, all hopped away through the soft snow,
out of sight. Uncle Wiggily tossed a snowball at the door, making a
sound like a knock, and then the bunny gentleman also hopped
away, laughing to himself.
He turned back in time to see the door open and Sister, Brother,
Daddy and Mother rush out.
"Oh, here's Bumps, now!" cried Brother. "We must have forgotten
and left her at the hill."
"Oh, that's what we did!" exclaimed Sister.
"Yes, but how did she get home?" asked Mother. "She never walked,
I'm sure!"
"And look at the queer wooden sled!" said Sister.
"Who brought you home, Bumps?" asked Daddy.
"A—a nice bunny man, and some little bunnies, and squirrels, and a
little bear boy and some ducks and chickens and little lambs and—
and——" But Bumps was out of breath now.
"Oh, she's been asleep and dreamed this!" laughed Brother. "Some
man must have found her and put her on this board for a sled, to
bring her home."
"Nope!" declared Bumps, "it was a bunny! It was a funny bunny!"
"Bring her in the house!" laughed Mother. "She must have been
dreaming!"
But we know it wasn't a dream; don't we? And if the strawberry
shortcake doesn't go swimming with the gold fish in the lemonade
and catch cold, I'll tell you next about Uncle Wiggily and the picnic.
STORY XV
UNCLE WIGGILY'S PICNIC

"Come on, Uncle Wiggily! Wake up! Wake up!" called Nurse Jane
Fuzzy Wuzzy in the hollow stump bungalow one morning. "Come on!"
"What's that? What's the matter? Is the chimney on fire again?"
asked the bunny gentleman, and he was so excited that he slid down
the banister, instead of hopping along from step to step as he should
have done.
"Of course the chimney isn't on fire!" laughed Miss Fuzzy Wuzzy. "But
this is the day for the picnic of the animal children, and you promised
to go with them to the woods."
"Oh, so I did!" exclaimed Uncle Wiggily, and he put one paw on his
pink nose to stop the twinkling, which started as soon as he grew
excited over thinking the chimney was on fire. "Well, I'm glad you
called me, Nurse Jane. I'll get ready for the picnic at once. What are
you going to put up for lunch?"
"Oh, some carrot bread, turnip cookies, lettuce sandwiches and nut
cake," answered the muskrat lady.
"That sounds fine!" laughed Uncle Wiggily. "I'm very glad I'm going
to the picnic!"
"Well, you had better hurry and get ready," remarked Miss Fuzzy
Wuzzy. "Here come Jackie and Peetie Bow Wow to see if you aren't
soon going to start."
Uncle Wiggily looked from the window of his hollow stump bungalow,
and saw the two little puppy dog boys coming along.
Jackie was so excited that he stubbed his paw and fell down twice,
while Peetie was so anxious to show Uncle Wiggily what was in the
package of lunch the puppies were going to take to the woods, that
Peetie fell down three times, and turned a back somersault.
"Uncle Wiggily! Uncle Wiggily! Aren't you coming?" barked Jackie.
"Hurry or it may rain and spoil the picnic," added Peetie.
"Oh, I hope not!" answered the bunny gentleman. "For if there is one
thing, more than another, that spoils a picnic, it is rain! Snow isn't so
bad, for we don't have picnics when it snows."
"Maybe it won't rain," hopefully spoke Nurse Jane, who was busy
putting up lunch for Uncle Wiggily. "There isn't a cloud in the sky!"
And, surely enough, when Uncle Wiggily, Nurse Jane and dozens of
animal children started off to the woods for their picnic, the sun
shone bravely down from the blue sky and a more lovely day could
not have been wished for.
The forest where the bunny gentleman, Nurse Jane and the animal
children went for their picnic was a large one, with many trees and
bushes. There were dozens of places for the squirrels, rabbits, goats,
ducks, dogs, pussy cats and others to play; and when they reached
the grove they put their lunches under bushes, on the soft cool,
green moss and began to have fun.
"Oh, Uncle Wiggily! Please turn skipping rope for us?" begged
Brighteyes, the little guinea pig girl.
"And please come play ball with us!" grunted Curly and Floppy
Twistytail, the piggie boys.
"Have a game of marbles with us," teased Billie Wagtail, the goat,
and Jacko Kinkytail, the monkey chap.
"I'll play with you all in turn," laughed the bunny gentleman. He was
in the midst of having fun, and was just gnawing off a piece of wild
grape vine to make a swing for Lulu and Alice Wibblewobble, the
ducks, when up came hopping Bully No-Tail, the frog boy. Bully was
quite excited.
"What's the matter, Bully?" asked Uncle Wiggily.
"Oh, gur-ump!" croaked Bully. "There is a big crowd of boys and girls
over on the other side of the pond. They're having a picnic, too! Ger-
ump! Ger-ump!"
"Real boys and girls!" added Bawly, who was Bully's brother. "Hump-
bump!"
"Well, that will do no harm!" laughed Uncle Wiggily. "Let the real boys
and girls have their picnic. They will not see us, for very few boys and
girls know how to use their eyes when they go to the woods. I have
often hidden beside a bush close to where a boy passed, and he
never saw me. Let the boys and girls have their picnic, and we'll have
ours!"
So that's the way it was. Uncle Wiggily and the animal children played
tag, and they slid down hill. Perhaps you think they could not do this
in summer when there was no snow. But the hills in the forest were
covered with long, smooth, brown pine needles, and these layers of
needles were so slippery that it was easy to slide on them.
And then, all of a sudden, just about when it was time to eat lunch, it
began to rain! Oh, how hard the drops pelted down! Rain! Rain! Rain!
"Scurry for shelter—all of you!" cried Nurse Jane. "Get out of the
rain!"
The animal boys and girls knew how to take care of themselves in a
rain storm, even if they had no umbrellas. Most of them had on fur or
feathers which water does not harm. And they snuggled down under
trees and bushes, finding shelter and dry spots so that, no matter
how hard it poured, they did not get very wet.
They hid their lunches under rocks and overhanging trees so nothing
was spoiled. And when the rain was over and the sun came out, as it
did, the animal picnic went on as before, and when the food was set
out on flat stumps for tables, there was enough for everyone, and
plenty left over.
Nurse Jane was looking at what remained of the good things to eat
when Jackie Bow Wow, who, with Peetie, had been splashing in a
mud puddle, came running up wagging his tail.
"Oh, Uncle Wiggily!" barked Jackie. "What you think? Those real
children, on the other side of the wood, they had their things to eat
out on some stumps for tables, just as we had, and when the rain
came, oh! it spoiled everything!"
"They didn't know how to keep their lunches dry," added Peetie.
"Now they haven't anything to eat for their picnic, and they are
starting home, and some of the little girls are crying."
"That's too bad!" murmured Uncle Wiggily, kindly. "Too bad that the
rain had to spoil their picnic! Now we have plenty of things left that
children could eat—nuts, apples, some popcorn and pears," for the
animal folk had brought all these, and many more, to the woods with
them. "We have lots left over."
"We could give them something to eat," spoke Nurse Jane, "but how
are we going to get it to them? We can't call them here; and it would
never do to let them see us carrying the things to them."
"No," agreed Uncle Wiggily. "But I think I have a plan. We can make
some baskets of birch bark. Some of the animal children—such as
Jacko and Jumpo Kinkytail, the monkeys, Joie and Tommie Kat,
Johnnie and Billie Bushytail, the squirrels—are good tree climbers. Let
them climb trees near where the real children are having their picnic,
and lower to them, on grape-vine ropes, the food we have left."
"Oh, yes!" mewed Tommie, the kitten boy. "What jolly fun!"
Quickly Nurse Jane began to gather up the food. Uncle Wiggily put it
in birch bark baskets the animal children made and then, with the
baskets, fastened to vines, in their paws or claws, the animal boys
went through the wood to the place of the other picnic. Uncle Wiggily
and the remaining animal children followed.
There the poor, disappointed real children were, looking at their rain-
soaked and spoiled lunches. Some of the little girls were crying.
"We might as well go home," grumbled a boy. "Our picnic is no
good!"
"Mean old rain!" sighed a girl.
But just then the animal chaps with lunch from Uncle Wiggily's picnic
—lunch which had not been rained on—climbed up into trees over the
heads of the boys and girls. Not a sound did the animal chaps make.
And when the real boys and girls had their backs turned, there were
lowered to the stump tables enough good things for a jolly feast—
apples, pears, popcorn, nuts and many other dainties.

A little girl happened to turn around and see the birch bark baskets of
good things just as the animal boys scurried off through the trees.
"Oh, look!" cried the girl. "The fairies have been here! They have left
us some lunch in place of ours that the rain spoiled. Oh, see the fairy
lunch!"
And I suppose that is as good a name for it as any, since the boys
and girls didn't see Uncle Wiggily's friends lower the baskets from the
trees. And the real boys and girls ate the lunch and had a most jolly
time, and so did the bunny gentleman and his picnic crowd.
Now if the rubber plant doesn't stretch over and tickle the teapot so
that it pours coffee instead of milk into the sugar bowl, you may next
hear about Uncle Wiggily in the rain storm.
STORY XVI
UNCLE WIGGILY'S RAIN STORM

Down pelted the rain in Animal Land.


It also poured in Boy and Girl Land, which was on the other side of
the forest from where Uncle Wiggily Longears lived in his hollow
stump bungalow.
The bunny rabbit gentleman looked out of a window, and saw the
drops fall drip, drip, dripping from trees and bushes, making little
puddles amid the leaves where birds could come, later, and take a
bath.
"You aren't thinking of going out in this storm; are you?" asked
Nurse Jane Fuzzy Wuzzy, the muskrat lady bungalow-keeper, as she
saw Mr. Longears putting on his coat.
"Why, I was, yes," slowly answered the bunny gentleman. "I am
neither sugar nor salt, that I will melt in the rain. And, as it isn't
freezing, I think I'll take a hop through the woods, and see
Grandfather Goosey Gander."
"Well, as long as you are going out, I wish you'd go to the store for
me," requested Miss Fuzzy Wuzzy.
"What do you want?" asked the bunny gentleman.
"Oh, bring a muskmelon for dinner," said Nurse Jane.
"A watermelon would be much easier to carry through the rain,"
Uncle Wiggily answered. "I think I'll bring a watermelon. If it gets
wet no harm is done."
"All right," agreed Nurse Jane, laughing, so away hopped the bunny
rabbit uncle, over the fields and through the woods. It seemed to
rain harder and harder, but Uncle Wiggily did not mind. He had an
umbrella, though he did not always carry one. It was made from a
toadstool, and it kept off most of the rain. Though, as Mr. Longears
said, he was neither a lollypop nor an ice-cream cone that would
melt in a shower.
But not everyone was as happy as Uncle Wiggily in this storm. On
the other side of the forest, as I told you, was Boy and Girl Land,
and in one of the houses lived a brother and a sister. They, too,
stood at the window, pressing their noses against the glass as the
rain beat down, and they were not happy.

"Rain, rain, go away!


Come again some other day!
Brother and I want to go and play!"

That is the verse the little girl recited over and over again as she
watched the rain pelting down. But the storm did not stop for all that
she said the verse backward and frontward.
"Will it ever stop?" crossly cried the boy. "Why doesn't it stop?" and
he drummed on the window sill, banged his feet on the floor and
whistled. And his sister loudly recited over and over again:
"Rain, rain, go away!"
"Children! Children!" gently called Mother from where she was lying
down in the next room. "Can't you please be a little quiet? My head
aches and I am trying to rest. The noise makes my pain worse."
"We're sorry, Mother," said the girl.
"But being quiet isn't any fun!" grumbled the boy. "Why can't we go
out and play?"
"Because you would get all wet," answered his mother. "I've told you
that two or three times, dear. Now please be quiet. It will stop
raining sometime, and then you may go out."
"What can we play with?" asked the boy, not very politely I'm sorry
to say.
"Why, some of your toys," replied his mother. "Surely you have
enough."
"I'm tired of 'em!" grunted the boy.
"So'm I," echoed his sister.
Then she began once more to say the verse about the rain, as if that
would do any good, and the boy rubbed his nose up and down the
window, making queer marks.
Uncle Wiggily, on his way to see Grandpa Goosey Gander, and get a
watermelon for Nurse Jane, took a short cut through a field, and
passed the house where the children were kept in on account of the
rain. And, as it happened, the window near which the boy and girl
stood was open a little way at the top.
So, as the bunny gentleman hopped past, he not only saw the
children, but he heard what they said, being able, as I have before
related to you, to understand real talk.
But the children were looking up at the sky so intently, trying to see
if it would stop raining, that they never noticed Uncle Wiggily.
Though if they had seen him, all dressed as he was like a gentleman
from the moving pictures, they would have been very much
surprised.
"Too bad those children have to stay in on account of the rain,"
thought Uncle Wiggily. "I wonder if I couldn't find some way of
amusing them? If they are tired of their own playthings I might toss
in, through the open window, some of the things the animal boys
and girls play with. I'll do it!"
Off through the woods in the rain hopped Uncle Wiggily. He found a
number of smooth, brown acorns, some of which had the cups, or
caps still on. He filled one pocket with the acorns.
Next the bunny picked up some cones from the pine tree. There
were large and small cones, and Nurse Jane always used one as a
nutmeg grater, it was so rough, while Uncle Wiggily kept one near
his bed to scratch his back at night.
"Let me see, what else would the animal children take?" said the
bunny to himself. "I think they would take some green moss, and
the girls would make beds with it for their dolls. The animal boys
would take hollow reeds and blow little pebbles through them as real
boys blow beans in their tin shooters. I'll take some moss and
reeds."
This the bunny uncle did, also picking up some empty snail and
periwinkle shells he found on the bank of a brook.
"The little girl can string these shells for beads," thought the bunny.
"And I'll strip off some pieces of white birch bark so the boy can
make a little canoe, as the Indians used to do."
Having gathered all these things—playthings which the animal
children found in the woods every day—the bunny hopped back to
the house of the boy and girl. The window was open, but the boy
and girl had left it. The girl was giving her mother a drink of water,
and the boy was bringing up some coal for the fire.
"This is my chance!" thought Uncle Wiggily.
Standing outside, he tossed in through the open window the acorns,
the pine cones, the shells, the moss and other things. Then he
hopped quickly away and hid behind a bush. He could hear the
children come back into the room, and soon he heard the girl cry:
"Oh, look what the wind blew in! Some acorns! I can make little
cups of them, and use the tops for saucers! And I'll set a play-party
table for my doll, and decorate it with green moss. Oh, how perfectly
lovely!"
"I'm going to make a boat out of this birch bark!" cried the boy. "And
look! A hollow reed, like a bean blower! Now I can have some fun!"
"Look at the lovely shells I can string and make a necklace of!" went
on the girl.
"And I can make wooden legs, and a wooden head and stick em on
these pine cones and make believe they're Noah's ark animals!"
laughed the boy. "Hurray!" he cried most happily.
"What is going on out there?" called Mother from where she was
lying down. "Have you found something to play with?"
"Yes'm," answered the boy. "We'll be quiet now."
"And we don't care if it does rain," said the girl. "The wind blew a lot
of lovely things in the window!"
But of course we know that Uncle Wiggily tossed them in.
"I guess they'll be all right now, no matter how much it rains," said
the bunny, as he hopped along to see Grandpa Goosey, and buy the
snowmelon—excuse me, I mean the watermelon—for Nurse Jane.
So this teaches us that sometimes a rain storm is good for letting
you find out new ways of having fun. And if the looking-glass doesn't
make funny faces at the rag doll, when she's trying to see if her hair
ribbon is on backward, on the next page you may read about Uncle
Wiggily and the mumps.
Note
Uncle Wiggily specially requests that the following story will NOT be
read to children who have the mumps. Please wait until they are
better.
STORY XVII
UNCLE WIGGILY AND THE MUMPS

Uncle Wiggily Longears, the bunny rabbit gentleman, was hopping


through the woods one day, and he was thinking of making his way
over to the other side of the forest, where the real boys and girls
lived, hoping he might have an adventure, when, all at once, Mr.
Longears heard some voices talking behind a mulberry bush.
"I know what we can do," said the voice of a boy, as Uncle Wiggily
could tell, for he had learned to know the talk of boys and girls.
"What can we do?" asked the voice of another boy.
"We can pick up a lot of stones," went on the first boy, "and we can
make believe we're hunters, and we can walk through the woods and
throw stones at the birds, and squirrels, and rabbits! Come on! Let's
do it!"
"Oh, no! I don't want to do that," said the second boy. "It isn't any
fun to throw stones at birds and bunnies. If you hit a mother bird,
and break her wing, she can't take anything to eat to the little birds,
and they'll starve."
"Pooh! That's nothing!" exclaimed the first boy, and Uncle Wiggily
peeked over the top of the bush to see what manner of boys these
were. But the bunny rabbit gentleman kept himself well hidden.
"I don't want any stones thrown at me," he thought.
"And," went on the second boy, who seemed rather kind, "if you
throw a stone at a rabbit you might break its leg, and then it couldn't
hop home to the baby rabbits."
"That is very true!" thought Uncle Wiggily, who was listening to all
that went on. "I wish there were more boys like this kind one."
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookname.com

You might also like