Programming JavaScript Applications
With this digital Early Release edition of Programming JavaScript Applications, you get the entire book bundle in its earliest form—the author's raw and unedited content—so you can take advantage of this content long before the book's official release. You'll also receive updates when significant chProgramming JavaScriptApplicationsEric elliottBeijing· Cambridge.mham·Kh· Sebastopol. TokyoOREILLYProgramming JavaScript Applicationsby Eric ElliottCopyright O 2014 Eric Elliott. All rights reservedPrinted in the United States of americaPublished by o reilly media, InC, 1005 Gravenstein Highway North, Sebastopol, CA95472O Reilly books may be purchased for educational, business, or sales promotional use Online editions arealsoavailableformosttitles(http://my.safaribooksonline.com).fOrmoreinformation,contactourcorporateinstitutionalsalesdepartment800-998-9938orcorporate@oreilly.comEditors: Simon St Laurent and Meghan blanchette Indexer: Lucie HaskinsProduction editor: Kara ebrahimCover Designer: Randy ComerCopyeditor: Eliahu SussmanInterior Designer: David FutatoProofreader: Amanda Kerseystrator: Rebecca demarestJuly 2014First editionRevision history for the First Edition:2014-06-25: First releaseSeehttp://oreilly.com/catalog/errata.csp?isbn=9781491950296forreleasedetailsNutshell Handbook, the Nutshell Handbook logo, and the O Reilly logo are registered trademarks ofO ReillyMedia, Inc. Programming JavaScript Applications, the image of an argali, and related trade dress are trademarks of o Reilly media, IncMany of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and O Reilly Media, Inc was aware ofa trademarkclaim, the designations have been printed in caps or initial capsWhile every precaution has been taken in the preparation of this book, the publisher and author assume noresponsibility for errors or omissions, or for damages resulting from the use of the information containedhereinISBN:978-1-491-95029-6Table of contentsPreface1. The JavaScript Revolution.Advantages of JavaScriptPerformanceObjectsSyntaxFirst-Class functionsEventsReusabilityThe Net resultAnatomy of a Typical Modern Java Script AppInfrastructureJSON: Data Storage and CommunicationNOSQL Data Stores22233344445669RESTful json Web services2. FunctionsMinimize side effects10Function Definition12Named Function Expressions14Lambdas16Immediately Invoked Function Expressions18Method Context20Function Scope22Hoistingg22Closures25Method Design27Named Parameters28Function Polymorphism29Generics and Collection Polymorphism32Method Chaining and Fluent aPis35Functional Programming36Stateless Functions(aka Pure Functions38Partial Application and CurryingAsynchronous operations41Callbacks41Promises and Deferreds42Conclusion3. ObjectsClassical Inheritance Is obsolete48Fluent-Style javaScrip51PrototypesDelegate Prototypes53Prototype Cloning56The Flyweight PatternObject Creation59Factories61Prototypal Inheritance with Stamps64Conclusie4. ModulesPrinciples of modularityInterfaces73The Module patternAsynchronous Module definition79Plug-I81Node- Style modules82nom84ES6 Modules86Building Client-Side Code with CommonJS, npm, Grunt, and Browserify87Defining the App87Feature Implementation90Bundling and deployment93Conclusion5. Separation of Concerns.99Client-Side Concerns100Module management101Events106Table of contentsModel View Controller/Mv*114Presentation and dom manipulation117Server-Side Concerns125Getting Started with Node and express125Conclusion1346. Access control137Authentication137Passwords137Credentia141Multifactor Authentication143Federated and Delegated Authentication145Authorization147Authorizing Applications148OAuth 2.0149Conclusion1507. Logging151Debugging151Server operations153Securit153Auditi154Business analytics154Viral factor155Logging checklist155Logging requests156Logging errors160Sample Log Output162Logging Service Alerts165Logging goals166Profiling and Instrumentation166Logging Client-Side Events167Deciphering Data168Conclusion1698. Building RESTful APls.171Usable172Focus172Consistency174Self-Describing: Hypermedia182Affordances182HATEOAS183Table of contentHTML as an API Media Type185185Jiron187Responsive apis189Optimizing for speed191Conclusion1929. Feature Toggle.193Organizing Features193Scale of a feature193Feature Groups194Liifespan of a Featuresp195Development195Staging195Production Testing195Feature rollout195Default activation196Full Integration196mplementation196Conclusion20010. Internationalization203Conclusion205A. JavaScript Style GuideIndex229ⅵi| Table of contentsPrefaceIntroductionThere are many books on the web technologies covered in this publication. However,there are precious few on Java Script that can be recommended to somebody who wantsto learn how to build a complete JavaScript application from the ground up. Meanwhilealmost every new tech startup needs knowledgeable JavaScript application developerson staff. This book exists for one purpose to help you gain the knowledge you need tobuild complete Java Script applications that are easy to extend and maintainThis book is not intended to teach you the basics of JavaScript. Instead, it's designed tobuild on your existing knowledge and discuss java Script features and techniques thatwill make your code easier to work with over time. Normally, as an application growsit becomes increasingly difficult to add new features and fix bugs. Your code becomestoo rigid and fragile, and even a small change could necessitate a lengthy refactor. If youfollow the patterns outlined in this book, your code will remain flexible and resilientChanges to one piece of code wont negatively impact anotherThis book will focus primarily on client-side architecture, although it will also coverserver-side topics, such as basic RESTful APIs and Node. The trend is that a great dealof the application logic is getting pushed to the client. It was once the case that the serverenvironment would handle things like templating and communication with vendorservices. Now, it's common to deal with both of those jobs inside the browserIn fact, a modern Java Script application does almost everything a traditional desktoppp would do completely in the browser. Of course, servers are still handy Server rolesfrequently include serving static content and dynamically loaded modules, data persistence, action logging, and interfacing with third-party APisWell coverJavaScript features and best practices for application developersCode organization, modularity, and reuseSeparation of concerns on the client side(mvc, etc.Communicating with servers and APIsDesigning and programming RESTful APIs with Node jsBuilding, testing, collaboration, deployment, and scalingExpanding reach via internationalizationWho this book ls forYou have some experience with JavaScript; at least a year or two working frequently withthe language, but you want to learn more about how you can apply it specifically todeveloping robust web-scale or enterprise applicationsYou know a thing or two about programming, but you have an insatiable thirst to learnmore. In particular, you'd like to learn more about how to apply the powerful featuresthat distinguish JavaScript from other languages, such as closures, functional program-ming, and prototypal inheritance(even if this is the first you've heard of themPerhaps you'd also like to learn about how to apply test-driven development (tddtechniques to your next JavaScript challenge. This book uses tests throughout the codeexamples. By the time you reach the end, you should be in the habit of thinking abouthow you'll test the code you writeWho this book ls not forThis book covers a lot of ground quickly. It was not written with the beginner in mindbut if you need clarification, you might find it in JavaScript: The Good Parts, by DouglasCrockford (O Reilly, 2008), JavaScript: The Definitive Guide, by David Flannagan(OReilly, 2011), or for help with software design patterns, the famous Gang of Fourbook (goF), Design Patterns: Elements of reusable Object-Oriented Software, by erichGamma, Richard Helm, Ralph Johnson, and John Vlissides(Addison-Wesley, 1994)Google and Wikipedia can be handy guides to help you through, as well. Wikipediaa fairly good reference for software design patternsIf this is your first exposure to Java Script, you would do well to study some introductorytexts and tutorials before you attempt to tackle this book. My favorite is Eloquent Java-Script by Marijn Haverbeke(No Starch Press, 2011). Be sure to follow that up withPreface
下载地址
用户评论
非常好得书