Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Building a JavaScript Library

60,416 views

Published on

This is the Google Tech Talk that I gave August 17th, 2007 on building a JavaScript library. I derived much of the talk from my experiences in building the jQuery and FUEL JavaScript libraries.

Published in: Business, Technology
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • PumaToto | Agen Togel Terpercaya PumaToto merupakan Togel Online | Togel Terpercaya dengan Prediksi Togel Akurat | Prediksi Angka Jitu Bagi kalian yang suka bermain Togel Singapore | Togel HK bisa untuk bergabung dengan PumaToto Cara Main Togel | Cara Pasang Togel | Pasang Togel Online di PumaToto, dengan Discount dan Hadiah Menarik Tentunya Daftarkan diri Anda bersama PumaToto https://caramaintogel.com/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • PumaToto | Agen Togel Terpercaya PumaToto merupakan Togel Online | Togel Terpercaya dengan Prediksi Togel Akurat | Prediksi Angka Jitu Bagi kalian yang suka bermain Togel Singapore | Togel HK bisa untuk bergabung dengan PumaToto Cara Main Togel | Cara Pasang Togel | Pasang Togel Online di PumaToto, dengan Discount dan Hadiah Menarik Tentunya Daftarkan diri Anda bersama PumaToto https://caramaintogel.com/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Building a JavaScript Library

  1. 1. Building a JavaScript Library John Resig (ejohn.org) jQuery JavaScript Library / Mozilla Corporation August 18th, 2007 - Google Tech Talk
  2. 2. jQuery Released Jan. 2006 ✦ Focus on DOM Traversal ✦ Built in support for Events, Ajax, and ✦ Animations Succinct code, small file size ✦ Extensible via a plugin architecture ✦
  3. 3. jQuery Samples $(“#main div”).addClass(“test”); ✦ $(“#main”).slideDown(“slow”); ✦ $(“ul > li”).click(function(){ ✦ $(this).find(“ul”).toggle(); }); $(“#contents”).load(“doc.html”); ✦
  4. 4. Doing something right? ~25 people on the jQuery Team ✦ 1/4 million visitors per month ✦ Fantastic Growth (via Google Trends) ✦
  5. 5. FUEL To be included in Firefox 3 ✦ A JavaScript library for extension ✦ developers Provides helpers for: Browser tabs, ✦ bookmarks, events, preferences Written in pure JavaScript, extensible ✦
  6. 6. FUEL Samples Application.prefs.all.forEach(function(p){ ✦ if ( p.modified ) // do something }); Application.events.addListener(“quit”, fn); ✦ Application.browser ✦ .open(quot;http://google.com/quot;).active = true; Application.bookmarks.all.forEach(function(cur){ ✦ if ( cur.url.match(/google.com/) ) cur.remove(); });
  7. 7. Need to know: Writing a Solid API ✦ Implementation ✦ Complex Applications ✦ Browser Bugs ✦ Documentation ✦ Testing ✦ Maintenance ✦
  8. 8. Writing A Solid API
  9. 9. Orthogonal Perform Universal Actions ✦ CRUD your methods! ✦ add/remove/delete/modify FUEL has the following on every object: ✦ ✦ .all, .add(...), .remove(...), .get(...) jQuery was missing .removeAttr() for the ✦ first couple months (oops!) Made a grid for FUEL, filled in the blanks ✦
  10. 10. Fear Adding Methods Methods can cause a support nightmare ✦ Avoid adding, if you can ✦ Defer to extensibility ✦ jQuery: ✦ ✦ Added .top(), .left(), .background() ✦ Duplicate of .css() - unnecessary
  11. 11. Embrace Removing Code Remove un-used code ✦ Reduces the size of your API ✦ Reduces your filesize ✦ Make your code more maintainable ✦ jQuery: Ran a poll to remove CSS 3 ✦ selectors. Reduced size of the API by 47% in 1.1 ✦
  12. 12. Provide an Upgrade Path Expect compatibility changes ✦ Provide transitional plugin ✦ Plugin for jQuery 1.1, for jQuery 1.0 users ✦ (+ documented in 3 languages)
  13. 13. Reduce to a common root Look for common patterns ✦ Reduce to its core and build up ✦ jQuery: ✦ ✦ .eq(0), .gt(3), and .lt(2) ✦ why not just implement slice? .slice(0,1) or .slice(1,4)
  14. 14. Consistency Users can “expect” good naming ✦ Pick a naming scheme and stick with it ✦ ✦ .click() vs .onclick() Argument position ✦ ✦ .method( options, arg2, ..., callback ) Callback context ✦ ✦ .method(function(){ // this == DOMElement });
  15. 15. Implementation
  16. 16. Evolution of a JavaScript Coder “Everything is a reference!” ✦ “You can do OO code!” ✦ “Huh, so that’s how Object Prototypes ✦ work!” “Thank God for closures!” ✦
  17. 17. Functional Programming Closures are essential ✦ Understand this snippet: ✦ (function(){ ✦ // your code... })(); Great for ‘local variables’ ✦ Perfect for macros ✦ var event = [‘click’,’focus’,’blur’,...]; ✦ jQuery.each(event,function(i,name){ jQuery.prototype[name] = function(fn){ return this.bind(name,fn); }; });
  18. 18. Quick Tip: Local Vars (function(){ ✦ // your code... var test = false; this.method(function(){ return test; }); }).call(this); Locally-scoped variables ✦ Access to instance ✦
  19. 19. Encapsulation Contain all of your code to a scope ✦ Hide your code and prevent it from ✦ leaking All of your code should be wrapped in a: ✦ (function(){ ✦ // your code... })(); BONUS! Your code compresses really well ✦ with Dojo Compressor, et. al.
  20. 20. Namespacing Use as few global variables as feasible ✦ One namespace is optimal ✦ (see: Dojo, Yahoo UI, jQuery, MochiKit) Questions to ask: ✦ ✦ Can my code coexist with other random code on the site? ✦ Can my code coexist with other copies of my own library? ✦ Can my code be embedded inside another namespace?
  21. 21. Don’t Extend Native Objects Down this path lies great pain and ✦ suffering Impossible to get DOM extensions ✦ working cross-browser Object.prototype kills kittens ✦ JS 1.6 methods cause conflicts: ✦ ✦ elem.getElementsByClassName ✦ array.forEach, .map, .filter
  22. 22. Perform Type Checking Make your API more fault resistant ✦ Coerce values wherever possible ✦ ✦ .css(Number) becomes: .css(Number + “px”) becomes: .map(String) ✦ .map(new Function(String)) Error messages ✦ ✦ Quite useful ✦ Byte-costly ✦ Solution: Move to ‘debugging’ extension
  23. 23. Quick Tip for OO Tweak your Object constructor ✦ function jQuery(str, con){ ✦ if ( window == this ) return new jQuery(str, con); // ... } Make it easier for users: ✦ new jQuery(“#foo”) becomes: jQuery(“#foo”)
  24. 24. Quick Tip for Errors Never gobble errors ✦ Ignore the templation to try{...}catch(e){} ✦ Improves debug-ability for everyone ✦
  25. 25. Complex Applications
  26. 26. Extensibility Your code should be easily extensible ✦ ✦ Methods: jQuery.fn.method = fn; $(...).method(); Selectors: jQuery.expr[‘:’].foo ✦ = “...”; $(“:foo”) Animations: jQuery.easing.easeout ✦ = fn; .animate({height: 100}, “slow”, “easeout”); Write less, defer to others ✦ Makes for cleaner code ✦ Foster community and growth ✦
  27. 27. Pure OO Code Object-Oriented is only one answer ✦ JavaScript != Java ✦ Not a good solution ✦ ✦ At least not until JavaScript 2 ✦ Classes, Packages, etc.
  28. 28. Custom Events The answer lies in custom events ✦ Dojo, jQuery, Yahoo UI - just added to ✦ Prototype Components trigger events and listen for ✦ others ✦ .bind(“drag”,fn) ✦ .trigger(“refresh”)
  29. 29. Browser Bugs
  30. 30. The Quirksmode Problem Fantastic resource ✦ Tells you where problems are ✦ Doesn’t tell you how to fix them ✦ Need to focus on problem sets ✦ ✦ Events ✦ Get Attribute ✦ Get Element Style
  31. 31. Solving a Problem Some issues are solved in depth ✦ ✦ DOM Events ✦ DOM Traversal Many still require hard work: ✦ ✦ getAttribute ✦ getComputedStyle Permute your test cases, look for edge ✦ cases
  32. 32. When to run the fix Typical thought progression: ✦ ✦ “I’ll just look at the useragent” ✦ “I’ll just detect to see if an object exists” ✦ “I’ll just think of an elegant solution to the problem” ✦ “I’ll just look at the useragent”
  33. 33. Documentation
  34. 34. Structured Provide a clear format ✦ Users can build new views with it ✦ ✦ No API view is perfect jQuery users built: ✦ ✦ API browsers ✦ Cheat sheets ✦ Widgets ✦ Translations An API for your API! ✦
  35. 35. Users Want to Help Make barrier to helping very low ✦ Answer: Keep your docs in a wiki ✦ Only do this if you’ve already written all of ✦ your docs ✦ Wiki != Documentation Use templates to maintain structure ✦
  36. 36. Focus on Leverage Write the docs that will get the most ✦ benefit Rough Priority: ✦ ✦ User-centric API ✦ Plugin authoring ✦ Docs on writing docs ✦ Advanced plugin authoring
  37. 37. Write the Docs Yourself It isn’t glamorous, but it’s essential ✦ You must buckle-down and do it yourself ✦ Improves your longevity and uptake ✦
  38. 38. Testing
  39. 39. 1000% Essential Don’t trust any library that doesn’t have a ✦ test suite ✦ Good: Prototype, MochiKit, jQuery, Yahoo UI Write your own suite (they’re pretty easy) ✦ Be sure to handle async tests ✦ ✦ Ajax ✦ Animations Pass in all supported browsers ✦
  40. 40. Test-Driven Development Write test cases before you tackle bugs ✦ Find devs who love to write test cases ✦ Check for failures before commit ✦ Pure JS DOM (running in Rhino) can help ✦ spot obvious errors ✦ pre_commit hook in SVN, if you can *cough* Google Code *cough*
  41. 41. Future of Testing Distributed Multi-Browser Testing ✦ How it works: ✦ ✦ Report results back to central server ✦ Server pushes new tests/code to clients ✦ Repeat jQuery and Prototype are very interested ✦ in this (expect something soon) Test against browser nightlies ✦ ✦ See: JS lib test cases in Mozilla trunk
  42. 42. Maintenance
  43. 43. Tackling New Bugs Create a rapid test environment ✦ ✦ ./gen.sh bugnum (dom|ajax|selector|...) Your test suite must be passing ✦ Have good coverage ✦ Always test in all supported browsers to ✦ avoid regressions Check unsupported browsers before ✦ release ✦ Don’t want them to crash/error out
  44. 44. Use Your Community Use your community as a sounding board ✦ ✦ Gauge the usefulness of features Users are great for finding weird edge cases ✦ Pay attention to repeat questions ✦ ✦ Indicative of problem in API design ✦ or lack of documentation
  45. 45. Maintain Focus Very, very, important ✦ Users generally choose libraries for ✦ ideological reasons Breaking your ideology will alienate your ✦ users jQuery: Small, concise, code. Small ✦ download, light core, easily extensible. Use plugins to divert functionality from ✦ your core
  46. 46. More Details Contact Me: ✦ jeresig@gmail.com More details: ✦ ✦ http://ejohn.org/ ✦ http://jquery.com/ ✦ http://wiki.mozilla.org/FUEL
  47. 47. Fun
  48. 48. Fun With One Liners “I’m not suggesting that one-liners are the heart of all programming. But ✦ they can be the hook to get someone exploring. A single line of code simply shows that a language can be focused. And it lets a beginner get comfortable with the basic atoms.” - _why jQuery allows for fantastic one liners ✦
  49. 49. One-Liners $(quot;div.sectionquot;) ✦ $(quot;div.sectionquot;).removeClass(quot;sectionquot;).hide(); ✦ $(quot;div.sectionquot;) ✦ .find(quot;dtquot;) .addClass(quot;sectionquot;) .click(function(){ $(this).next().toggle(); }) .end() .find(quot;ddquot;) .hide() .filter(quot;:firstquot;) .show() .end() .end();
  50. 50. One-Liners Remove unsightly anonymous functions! ✦ $(quot;div.sectionquot;) ✦ .find(quot;dtquot;) .addClass(quot;sectionquot;) .onclick() .next().toggle().end() .end() .end() .find(quot;ddquot;) .hide() .filter(quot;:firstquot;) .show() .end() .end();
  51. 51. Domain-Specific Language $(quot;div.sectionquot;) ✦ find(quot;dtquot;) addClass(quot;sectionquot;) click( next() toggle() ) end() find(quot;ddquot;) hide() filter(quot;:firstquot;) show() end() end()
  52. 52. Domain-Specific Language $(quot;div.sectionquot; ✦ find(quot;dtquot; addClass(quot;sectionquot;) click( next( toggle()))) find(quot;ddquot; hide() filter(quot;:firstquot; show())))
  53. 53. Domain-Specific Language Lisp-y! ✦ ($ quot;div.sectionquot; ✦ (find quot;dtquot; (addClass quot;sectionquot;) (click (next (toggle)))) (find quot;ddquot; (hide) (filter quot;:firstquot; (show)))))
  54. 54. Domain-Specific Language Python-y! ✦ div.section: ✦ dt: addClass quot;sectionquot; click next toggle dd: hide :first: show Give it a try: ✦ http://ejohn.org/apps/jquery2/ Secret Sauce: ✦ <script type=”text/jquery”>...</script>

×