Pro Javascript Techniques (2nd Edition) by John Paxton, John Resig, and Russ Ferguson

17567831af8d59a.jpg Author John Paxton, John Resig, and Russ Ferguson
File size 3.9 MB
Year 2015
Pages 206
Language English
File format PDF
Category programming


Pro JavaScript Techniques Second Edition John Resig Russ Ferguson John Paxton Pro JavaScript Techniques Copyright © 2015 by John Resig, Russ Ferguson, and John Paxton This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. ISBN-13 (pbk): 978-1-4302-6391-3 ISBN-13 (electronic): 978-1-4302-6392-0 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director: Welmoed Spahr Acquisitions Editor: Louise Corrigan Technical Reviewer: Mark Nenadov, Ian Devlin Editorial Board: Steve Anglin, Mark Beckner, Gary Cornell, Louise Corrigan, James DeWolf, Jonathan Gennick, Robert Hutchinson, Celestin Suresh John, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing, Matt Wade, Steve Weiss Coordinating Editor: Rita Fernando Copy Editor: James Compton Compositor: SPi Global Indexer: SPi Global Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales– eBook Licensing web page at Any source code or other supplementary materials referenced by the author in this text is available to readers at For detailed information about how to locate your book’s source code, go to I will forever be grateful to my dad. Even now I’m still learning from his example. Dad, Rodd, we miss you both. Watching Doctor Who and The Twilight Zone has never been the same. —Russ Ferguson For Andreina, who always believed. —John Paxton Contents at a Glance About the Authors About the Technical Reviewers Acknowledgments Chapter 1: Professional JavaScript Techniques Chapter 2: Features, Functions, and Objects Chapter 3: Creating Reusable Code Chapter 4: Debugging JavaScript Code Chapter 5: The Document Object Model Chapter 6: Events Chapter 7: JavaScript and Form Validation Chapter 8: Introduction to Ajax Chapter 9: Web Production Tools Chapter 10: AngularJS and Testing Chapter 11: The Future of JavaScript Appendix A: DOM Reference Index Contents About the Authors About the Technical Reviewers Acknowledgments Chapter 1: Professional JavaScript Techniques How Did We Get Here? Modern JavaScript The Rise of Libraries More Than a Note about Mobile Where Do We Go from Here? Coming Up Next Summary Chapter 2: Features, Functions, and Objects Language Features References and Values Scope Context Closures Function Overloading and Type-Checking New Object Tools Objects Modifying Objects Summary Chapter 3: Creating Reusable Code Object-Oriented JavaScript Inheritance Member Visibility The Future of Object-Oriented JavaScript Packaging JavaScript Namespaces The Module Pattern Immediately Invoked Function Expressions Summary Chapter 4: Debugging JavaScript Code Debugging Tools The Console Leveraging the Console Features The Debugger DOM Inspector Network Analyzer Timeline Profiler Summary Chapter 5: The Document Object Model An Introduction to the Document Object Model DOM Structure DOM Relationships Accessing DOM Elements Finding Elements by CSS Selector Waiting for the HTML DOM to Load Waiting for the Page to Load Waiting for the Right Event Getting the Contents of an Element Getting the Text of an Element Getting the HTML of an Element Working with Element Attributes Getting and Setting an Attribute Value Modifying the DOM Creating Nodes Using the DOM Inserting into the DOM Injecting HTML into the DOM Removing Nodes from the DOM Handling White Space in the DOM Simple DOM Navigation Summary Chapter 6: Events Introduction to JavaScript Events The Stack, the Queue, and the Event Loop Event Phases Binding Event Listeners Traditional Binding DOM Binding: W3C Unbinding Events Common Event Features The Event Object Canceling Event Bubbling Overriding the Browser’s Default Action Event Delegation The Event Object General Properties Mouse Properties Keyboard Properties Types of Events Page Events UI Events Mouse Events Keyboard Events Form Events Event Accessibility Summary Chapter 7: JavaScript and Form Validation HTML and CSS Form Validation CSS JavaScript Form Validation Validation and Users Validation Events Customizing Validation Preventing Form Validation Summary Chapter 8: Introduction to Ajax Using Ajax HTTP Requests HTTP Response Summary Chapter 9: Web Production Tools Scaffolding Your Projects NPM is the Foundation for Everything Generators Version Control Adding Files, Updates, and the First Commit Summary Chapter 10: AngularJS and Testing Views and Controllers Remote Data Sources Routes Route Parameters Application Testing Unit Testing End to End Testing with Protractor Summary Chapter 11: The Future of JavaScript The Once and Future ECMAScript Using ECMAScript Harmony Harmony Resources Working with Harmony ECMAScript Harmony Language Features Arrow Functions Classes Promises Modules Type Extensions New Collection Types Summary Appendix A: DOM Reference Resources Terminology Global Variables document HTMLElement DOM Navigation body childNodes documentElement firstChild getElementById( elemID ) getElementsByTagName( tagName ) lastChild nextSibling parentNode previousSibling Node Information innerText nodeName nodeType nodeValue Attributes className getAttribute( attrName ) removeAttribute( attrName ) setAttribute( attrName, attrValue ) DOM Modification appendChild( nodeToAppend ) cloneNode( true|false ) createElement( tagName ) createElementNS( namespace, tagName ) createTextNode( textString ) innerHTML insertBefore( nodeToInsert, nodeToInsertBefore ) removeChild( nodeToRemove ) replaceChild( nodeToInsert, nodeToReplace ) Index About the Authors John Resig is a developer at Khan Academy and the creator of the jQuery JavaScript library. In addition to Pro JavaScript Techniques, he’s also the author of Secrets of the JavaScript Ninja (Manning, 2012). John is a Visiting Researcher at Ritsumeikan University in Kyoto working on the study of Ukiyo-e (Japanese woodblock printing). He has developed a comprehensive woodblock print database and image search engine, located at Russ Ferguson is a developer and instructor working in and around New York City. He is currently a manager with SunGard Consulting Services, developing applications for clients like Morgan Stanley and Comcast. For many years Russ has been an instructor for Pratt Institute and Parsons School of Design. He has developed applications for both start-ups and established organizations like Chase Bank, Publicis Groupe, DC Comics, and MTV/Viacom. Some of his interests are encouraging young people to code and the ways technology changes media consumption and participation. Other interests include practicing Japanese, writing, film, concerts, and finding a good wine or sake. Tweets can be found @asciibn. John Paxton is a programmer, trainer, author, and presenter who lives in his native New Jersey. Studying history at Johns Hopkins University, he discovered that he spent more time in the computer lab than at the document archives. Since then, his career has oscillated between programming and training, working with many of the various languages used in web development over the last 15 years. He now concentrates on JavaScript and Java, with the occasional nostalgic visits to Perl and XML. He can be found on Twitter @paxtonjohn and at his website: About the Technical Reviewers Ian Devlin is interested in all things web, and currently works as a senior web developer at a web agency based in Düsseldorf, Germany. He is an HTML5 Doctor and a founding contributor to Intel’s HTML5 Hub and has written articles for a number of developer zones such as Mozilla, Opera, Intel, and Adobe, and for net magazine. He has also written a book on HTML5 multimedia and has been technical reviewer for a number of Apress books. Mark Nenadov is a software developer with around 15 years of experience, predominantly with open source technologies. Mark lives in Essex, Ontario, Canada with his lovely wife and their two adorable daughters—with a son on the way. When he’s not developing software or spending time with his family, he is usually hiking, observing wildlife, reading, researching history, reviewing/editing manuscripts, or writing. Mark is an avid poet and his poems have appeared in publications in the United States, Canada, Pakistan, India, Australia, England, and Ireland. Acknowledgments There are always a lot of people to thank: The good people at Apress, including Louise Corrigan, Rita Fernando, and Christine Ricketts. Without them I would not have had the opportunity to work on this. The other authors on this project, John Paxton and John Resig, whose knowledge and experience are in the pages of this book. Technical Reviewers make good books great, so thanks to Mark Nenadov and Ian Devlin for helping me clarify my intentions. Thanks to my family and friends, who have been very understanding while I disappear for hours at a time to sit in front of a computer for even more time than usual. —Russ Ferguson My contributions to this book would not have happened without the faith, patience, endurance, and guidance of Louise Corrigan and Christine Ricketts. They both put up with far more in delays and late chapters than any reasonable human being should have. —John Paxton CHAPTER 1 Professional JavaScript Techniques Welcome to Pro JavaScript Techniques. This book provides an overview of the current state of JavaScript, particularly as it applies to the professional programmer. Who is the professional programmer? Someone who has a firm grasp of the basics of JavaScript (and probably several other languages). You are interested in the breadth and depth of JavaScript. You want to look at the typical features like the Document Object Model (DOM), but also learn about what’s going on with all this talk of Model-ViewController (MVC) on the client side. Updated APIs, new features and functionality, and creative applications of code are what you are looking for here. This is the second edition of this book. Much has changed since the first edition came out in 2006. At that time, JavaScript was going through a somewhat painful transition from being a toy scripting language to being a language that was useful and effective for several different tasks. It was, if you will, JavaScript’s adolescence. Now, JavaScript is at the end of another transition: to continue the metaphor, from adolescence to adulthood. JavaScript usage is nearly ubiquitous, with anywhere from 85 to 95 percent of websites, depending on whose statistics you believe, having some JavaScript on their main page. Many people speak of JavaScript as the most popular programming language in the world (in the number of people who use it on a regular basis). But more important than mere usage are effectiveness and capability. JavaScript has transitioned from a toy language (image rollovers! status bar text manipulations!) to an effective, if limited tool (think of client-side form validation), to its current position as a broad-featured programming language no longer limited to mere browsers. Programmers are writing JavaScript tools that provide MVC functionality, which was long the domain of the server, as well as complex data visualizations, template libraries, and more. The list goes on and on. Where in the past, designers would have relied on a .NET or Java Swing client to provide a full-featured, rich interface to server-side data, we can now realize that application in JavaScript with a browser. And, using Node.js, we have JavaScript’s own version of a virtual machine, an executable that can run any number of different applications, all written in JavaScript and none requiring a browser. This chapter will describe how we got here and where we are going. It will look at the various improvements in browser technology (and popularity) that have abetted the JavaScript Revolution. The state of JavaScript itself needs inspection, as we want to know where we are before we look at where we are going. Then, as we examine the chapters to come, you will see what the professional JavaScript programmer needs to know to live up to his or her title. How Did We Get Here? As of the first edition of the book, Google Chrome and Mozilla Firefox were relatively new kids on the block. Internet Explorer 6 and 7 ruled the roost, with version 8 gaining some popularity. Several factors combined to jump-start JavaScript development. For most of its life, JavaScript was dependent upon the browser. The browser is the runtime environment for JavaScript, and a programmer’s access to JavaScript functionality was highly dependent upon the make, model, and version of browser visiting said programmer’s website. By the mid-2000s, the browser wars of the 90s had been easily won by Internet Explorer, and browser development stagnated. Two browsers challenged this state of affairs: Mozilla Firefox and Google Chrome. Firefox was the descendant of Netscape, one of the earliest web browsers. Chrome had Google’s backing, more than enough to make it an instant player on the scene. But both of these browsers made a few design decisions that facilitated the JavaScript revolution. The first decision was to support the World Wide Web consortium’s implementation of various standards. Whether dealing with the DOM, event handling, or Ajax, Chrome and Firefox generally followed the spec and implemented it as well as possible. For programmers, this meant that we didn’t have to write separate code for Firefox and Chrome. We were already used to writing separate code for IE and something else, so having branching code in itself was not new. But making sure that the branching was not overly complex was a welcome relief. Speaking of standards, Firefox and Chrome also put in a lot of work with the European Computer Manufacturer’s Association (ECMA, now styled Ecma). Ecma is the standards body that oversees JavaScript. (To be technical, Ecma oversees the ECMAScript standard, since JavaScript is a trademark of Oracle and… well, we don’t really care about those details, do we? We will use JavaScript to refer to the language and ECMAScript to refer to the specification to which a JavaScript implementation adheres.) ECMAScript standards had languished in much the same way as IE development. With the rise of real browser competition, the ECMAScript standard was taken up again. ECMAScript version 5 (2009) codified many of the changes that had been made in the ten years (!) since the previous version of the standard. The group itself was also energized, with version 5.1 coming out in 2011. The future is provided for, with significant work currently being done on both versions 6 and 7 of the standard. To give credit where credit is due, Chrome pushed the updating of JavaScript as well. The Chrome JavaScript engine, called V8, was a very important part of Chrome’s debut in 2008. The Chrome team built an engine that was much faster than most JavaScript engines, and it has kept that goal at the top of the list for subsequent versions. In fact, the V8 engine was so impressive that it became the core of Node.js, a browserindependent JavaScript interpreter. Originally intended as a server that would use JavaScript as its main application language, Node has become a flexible platform for running any number of JavaScript-based applications. Back to Chrome: the other major innovation Google introduced to the land of browsers was the concept of the evergreen application. Instead of having to download a separate browser install for updates, Chrome’s default is to automatically update the browser for you. While this approach is sometimes a pain in the corporate world, it is a great boon to the noncorporate consumer surfer (also known as a person!). If you use Chrome (and, for the last few years, Firefox), your browser is up-to-date, without your having to make any effort. While Microsoft has done this for a long time in pushing security updates via Windows Update, it does not introduce new features to Internet Explorer unless they are coupled to a new version of Windows. To put it another way, updates to IE are slow in coming. Chrome and Firefox always have the latest and greatest features, as well as being quite secure. As Google pressed on with Chrome’s features, the other browser makers played catch-up. Sometimes this came in sillier ways, such as when Firefox adapted Chrome’s version numbering. But it also resulted in Mozilla and Microsoft taking a cold, hard look at JavaScript engines. Both browser makers have significantly overhauled their JS engines over the last few years, and Chrome’s lead, while formidable, is no longer insurmountable. Finally, Microsoft has (mostly) thrown in the towel on its classic “embrace and extend” philosophy, at least when it comes to JavaScript. With IE version 9, Microsoft implemented World Wide Web Consortium (W3C) event handling and standardized its DOM interfaces as well as its Ajax API. For most of the standard features of JavaScript, we no longer have to implement two versions of the same code! (Legacy code for legacy browsers is still a bit of an issue, of course…) It seems almost a panacea. JavaScript is faster than ever before. It is easier to write code for a variety of different browsers. Standards documents both describe the real world and provide a useful roadmap to features to come. And most of our browsers are fully up-to-date. So what do we need to worry about now, and where are we going in the future?

Author John Paxton, John Resig, and Russ Ferguson Isbn File size 3.9 MB Year 2015 Pages 206 Language English File format PDF Category Programming Book Description: FacebookTwitterGoogle+TumblrDiggMySpaceShare This book is for the developer who wants to get the most out of their JavaScript. They may have purchased the first edition of this book and now want to get up-to-date with the latest developments, or this may be the first time they have come across the title. They could be a working developer or using JavaScript in their spare time – this book will enhance their skills and make them a better JavaScript developer. ro JavaScript Techniques is the ultimate JavaScript book for today’s web developer. It provides everything you need to know about modern JavaScript, and teaches you what JavaScript can do for your web sites. This book doesn’t waste any time looking at things you already know, like basic syntax and structures, but instead concentrates on fundamental, vital topics—what modern JavaScripting is (and isn’t), and pitfalls to be wary of. You will learn about the ‘this’ keyword, as well as new object tools. You will be able to create reusable code with encapsulation, overloading and inheritance. The most recent techniques for debugging and testing are covered comprehensively, with information on Chrome developer tools, Jasmine, QUnit, and PhantomJS. This update finishes with chapters on constructing the single-page web applications that dominate the modern web, and an overview of JavaScript libraries. The book is filled with real-world examples and case studies, as well as numerous reusable functions and classes to save you time in your development. You will learn the practical skills needed to build professional, dynamic web applications. Pro JavaScript Techniques is an indispensable reference for any professional JavaScript web developer – enhance your JavaScript development today. What you’ll learn Real-world techniques for the modern JavaScript developer How to enhance your development skills and be a better developer Efficient techniques for object-oriented JavaScript, including the ‘this’ keyword The most up-to-date tools for testing and debugging, including browser development tools How to effectively use events, forms and Ajax How to use JavaScript libraries     Download (3.9 MB) Understanding Oracle Apex 5 Application Development, 2nd Edition Ios App Development For Teens By Teens: Beginning Ios App Development Html5 Games Most Wanted: Build The Best Html5 Games Jquery In Action Pro Couchbase Development: A Nosql Platform For The Enterprise Load more posts

Leave a Reply

Your email address will not be published. Required fields are marked *