Pro JavaScript Design Patterns
As a web developer, you’ll already know that JavaScript™ is a powerful language, allowing you to add an impressive array of dynamic functionality to otherwise static web sites. But there is more power waiting to be unlocked--JavaScript is capable of full object-oriented capabilities, and by applyingPro JavaScriptDesign PatternsRoss harmes and dustin diazApress°Pro JavaScript" Design PatternsCopyright o 2008 by Ross Harmes and dustin diazAll 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 retrievalsystem, without the prior written permission of the copyright owner and the publisherISBN-13(pbk):978-1-59059-908-2ISBN-10(pbk):1-59059908XISBN-13( electronic:978-1-4302-0495-4ISBN-10(electronic): 1-4302-0495-8Printed and bound in the united states of america 987654321Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrenceof a trademarked name we use the names only in an editorial fashion and to the benefit of the trademarkowner, with no intention of infringement of the trademark.Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems Inc in theUnited States and other countries. Apress Inc is not affiliated with Sun Microsystems InC. and this bookwas written without endorsement from Sun Microsystems Inc.Lead editors Chris Mills, Tom WelshTechnical Reviewer: Simon willisonEditorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan GennickJason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper,Ben renow-Clarke, Dominic shakeshaft, Matt Wade. Tom WelshProject Manager: Richard Dal PortoCopy Editor: Jennifer WhippleAssociate Production Director: Kari Brooks-CoponyProduction Editor: Kelly WinquistCompositor and Artist: Kinetic Publishing Services, LLCProofreader: dan shawIndexer: Julie gradyCover Designer: Kurt KramesManufacturing Director: Tom DebolskiDistributed 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 orders-ny@springer-sbm com, orvisithttp://www.springeronline.comFor information on translations, please contact Apress directly at 2855 Telegraph avenue, Suite 600 BerkeleyCa94705.Phone510-549-5930,fax510-549-5939,e-mailinfo@apress.com,orvisithttp://www.apress.com.The information in this book is distributed on anas is"basis, without warranty. Although every precautionhas been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability toany person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectlyby the information contained in this workThesourcecodeforthisbookisavailabletoreadersathttp://www.apress.ceTo Mom, and those who have listened, thanksDustin diazTo Alec, Dymphi, and TerryRoss harmesContents at a glanceAbout the authorsAbout the technical reviewerAcknowledgmentsXIXIntroductionPART 1 Object-Oriented JavaScriptCHAPTER 1 Expressive JavaScriptCHAPTER 2 InterfacesCHAPTER3 Encapsulation and Information hiding面面25CHAPTER 4 Inheritance......,.41CHAPTER 5 The Singleton PatternCHAPTER 6 ChainingPART 2Design patternsCHAPTER 7 The Factory PatternCHAPTER 8 The bridge PatternCHAPTER 9 The Composite Pattern125CHAPTER 10 The facade patternCHAPTER 11 The Adapter Pattern.....149CHAPTER 12 The decorator pattern.159CHAPTER 13 The Flyweight Pattern179CHAPTER 14 The Proxy PatternCHAPTER 15 The observer pattern215CHAPTER 16 The command pattern225CHAPTER 17 The Chain of Responsibility Pattern.245INDEXContentsAbout the authorsAbout the technical reviewerAcknowledgmentsXIXIntroductionPART 1 Object-Oriented JavaScriptCHAPTER 1 Expressive JavaScriptThe Flexibility of JavaScriptA Loosely Typed LanguageFunctions As First-Class objectsThe mutability of objects8InheritanceDesign Patterns in JavaScriptSummarychaPTer 2 InterfacesWhat is an Interface?Benefits of Using InterfacesDrawbacks of Using Interfaces12How other Object-Oriented Languages Handle Interfaces....... 12Emulating an interface in JavascriptDescribing Interfaces with CommentsEmulating Interfaces with Attribute CheckingEmulating Interfaces with Duck TypingThe Interface Implementation for this bookThe Interface Class.19When to Use the interface classHow to Use the interface class20Example: Using the Interface ClassPatterns That rely on the InterfaceSummarCONTENTSCHAPTER 3 Encapsulation and Information HidingThe Information Hiding principleEncapsulation Vs Information HidingThe role of the interface55606Basic PatternsFully Exposed ObjectPrivate Methods Using a naming conventionScope, Nested Functions, and closuresPrivate Members Through ClosuresMore Advanced patternsStatic Methods and attributesConstantsSingletons and object Factories38Benefits of Using EncapsulationDrawbacks to Using Encapsulation39SummaryCHAPTER 4 InheritanceWhy do you need InheritanceClassical Inheritance42The prototype chai42The extend function43Prototypal InheritanceAsymmetrical Reading and Writing of Inherited MembersThe clone functionComparing Classical and Prototypal Inheritance49Inheritance and encapsulation49Mixin classesExample: Edit-in-Place52Using classical InheritanceUsing prototypal InheritanceUsing Mixin ClasWhen should inheritance Be Used?SummaryCHAPTER 5 The Singleton PatternThe Basic structure of the singletonNamespacing
用户评论