SlideShare una empresa de Scribd logo
1 de 66
Descargar para leer sin conexión
Javascript
                          The Interlingua of the Web
                               Anders Janmyr
                              http://anders.janmyr.com
                             anders.janmyr@jayway.com
                                  @andersjanmyr




Monday, October 4, 2010
Ugly
     Javascript




Monday, October 4, 2010
Beautiful
        Javascript




Monday, October 4, 2010
Y-Combinator
                             Scheme (Lisp)
                          (define Y
                            (lambda (X)
                              ((lambda (procedure)
                                 (X (lambda (arg)
                                   ((procedure procedure)
                                   arg))))
                               (lambda (procedure)
                                 (X (lambda (arg)
                                   ((procedure procedure)
                                   arg)))))))




Monday, October 4, 2010
Y-Combinator
                                Javascript
                          function Y (X) {
                            return (function(procedure) {
                              return X(function(arg) {
                                return procedure(procedure)(arg);
                              });
                            })(function(procedure) {
                              return X(function(arg) {
                                return procedure(procedure)(arg);
                              });
                            });
                          }




Monday, October 4, 2010
Object


                          Dynamic


Monday, October 4, 2010
Object


                          Hashtable


Monday, October 4, 2010
Object


                          Prototypical


Monday, October 4, 2010
Object Literal


                          var empty_object = {};




Monday, October 4, 2010
var kjell = {
                              name: "Kjell",
                              "kind": "Malayan"
                          };




Monday, October 4, 2010
var kjell = {
                         name: "Kjell",
                         "kind": "Malayan"
                         address: {
                             country: "Denmark"
                         }
                     };


Monday, October 4, 2010
Retrieval
                   kjell.name    // “Kjell”
                   kjell["name"] // “Kjell”

                   // Denmark
                   kjell.address.country
                   kjell['address']["country"]



Monday, October 4, 2010
Retrieving non-
                          existent properties
                kjell.firstname // undefined
                kjell["NAME"]   // undefined
                kjell.home.country // Error




Monday, October 4, 2010
Setting non-existent
                          properties
                          kjell.firstname = 'Sven';
                          kjell["NAME"] = 'SVEN';

                          kjell.firstname // 'Sven'




Monday, October 4, 2010
Prototypical Inheritance
             using Object.create
                          var rudolph =
                              Object.create(kjell);

                          rudolph.name   // “Kjell”




Monday, October 4, 2010
Prototypical
                          Inheritance
                                    name    Kjell
                                    kind   Malayan

                     id   a4r54ed
           prototype




Monday, October 4, 2010
rudolph.name = 'Rudolph';

                          rudolph.name // “Rudolph”
                          kjell.name   // “Kjell”

                          rudolph.kind // ‘Malayan’



Monday, October 4, 2010
rudolph.kind    // ‘Malayan’


                      kjell.kind = 'Baird';

                      rudolph.kind   // ‘Baird’




Monday, October 4, 2010
delete rudolph.name

                          rudolph.name // ‘Kjell’




Monday, October 4, 2010
Prototypical
                              Inheritance
                            new Constructor();

                          Returns a new object with a link to

                          Constructor.prototype




Monday, October 4, 2010
Prototypical
                          Inheritance
              Object.create = function(o) {
                  function F() {}
                  F.prototype = o;
                  return new F();
              }




Monday, October 4, 2010
Arrays

                    • Array inherits from object
                    • Indexes are converted to strings
                    • Magic length property
                          •   Always one larger than the highest int
                              property



Monday, October 4, 2010
Array Literals

                    •[]
                    • names =           [ ʻRudolphʼ, ʻKjellʼ,
                          ʻTorstenʼ]
                    • typeof value == ʻobjectʼ
                          •   value.constructor === Array



Monday, October 4, 2010
Array Methods
                    • concat
                    • join
                    • pop
                    • push
                    • slice
                    • sort
                    • splice
Monday, October 4, 2010
JSON
          {
                   "version": "1.0",
                   "markets": [
                       { "name": "Europe", "currency": "EUR"},
                       { "name": "North America", "currency": "USD"},
                       { "name": "Other", "currency": "USD"}
                   ],
                   "serviceTypes": ["Maintenence", "WearingPart"],
                   "valueTypes": ["Market value", "Trade in value"]
          }




Monday, October 4, 2010
Function

                          First class
                            object


Monday, October 4, 2010
Function


                           lambda
                          function() {};




Monday, October 4, 2010
Function Statement
                          function foo() {}

                          expands to

                          var foo = function foo() {};




Monday, October 4, 2010
Functions

                          Can be defined inside other functions!




Monday, October 4, 2010
residualValues: function(cur) {
       var self = this;
       return function() {
         return [1,2,3,4,5].map(function(age) {
             return {
                years: age,
                value: self.tradePercent(cur, age)
             };
         });
       }
     }


Monday, October 4, 2010
Methods

                          functions stored in objects




Monday, October 4, 2010
Built-in Prototypes
                    • Object.prototype
                    • Array.prototype
                    • Function.prototype
                    • Number.prototype
                    • String.prototype
Monday, October 4, 2010
Array.prototype.contains = function(element)
  {
      for (var i = 0; i < this.length; i++) {
          if (this[i] == element) return true;
      }
      return false;
  }


                          [1, 2, 3].contains(3); // true




Monday, October 4, 2010
Function.prototype.method =
                        function(name, func) {
                          this.prototype[name] = func;
                          return this;
                      }




Monday, October 4, 2010
String.method(‘trim’, function() {
            return this.replace(/^s+|s+$/g, ‘’);
          }




          “ tapir “.trim(); // “tapir”




Monday, October 4, 2010
Dynamics



Monday, October 4, 2010
Scope

               The function is the scope of the
                          variables




Monday, October 4, 2010
Invocation Forms
                    • •Function form
                        sleep(10)

                    • •Method form
                        kjell.sleep(10)
                          •   kjell[“sleep”](10)

                    • Constructor form
                      • new sleep(10)
                    • •Apply form
                        sleep.apply(rudolph, 10)


Monday, October 4, 2010
this
                                Invocation
            this is an extra                    this
                                   Form
               dynamic                       the global
            parameter that       function
                                               object
            depends on the       method         kjell
              calling form
                                constructor a new object

                                   apply      rudolph




Monday, October 4, 2010
arguments

                    • A special array like, DYNAMIC,
                          parameter


                    • All the arguments of the invocation

Monday, October 4, 2010
function sum() {
                 var i,
                     n = arguments.length,
                     total = 0;
                 for (i = 0; i < n; i += 1) {
                   total += arguments[i];
                 }
                 return total;
               }



                sum(1, 2, 3, 4);




Monday, October 4, 2010
Dynamic Compilation
                    • eval
                          •   Evaluates a string and returns the result.

                    • new Function(parameterArray,
                          codeString)
                          •   Creates and returns a function.
                          •   var add=new Function("a", "b", "return a+b;");




Monday, October 4, 2010
Global Object
                    • Container for all variables
                    • On browsers window == global
                    • Any var not declared is global
                    • Global variables are Dangerous

Monday, October 4, 2010
Good
                          Practices




Monday, October 4, 2010
Modules

                          var MyNamespace = {};


                          var MyNS = MyNS || {};




Monday, October 4, 2010
Cascade
                          setFirst: function(name) {
                            this.first = name;
                            return this;
                          }


                     person
                      .setFirst(“Anders”)
                      .setLast(“Janmyr”)
                      .setAge(40);


Monday, October 4, 2010
Encapsulation
                          var Esperanto = Esperanto || {};

                          Esperanto.Lab = function() {
                              var privVar = "example";
                              function privFunc() {
                                  return privVar;
                              }

                                return {
                                    example: function() {
                                         return privFunc();
                                    }
                                }
                          }()




Monday, October 4, 2010
Local Functions
                 costData: function(current) {
                     var data = {};
                     function addEntry(name, cost) {
                         data[name + "PerHour"] = model.withTwoDec(cost/hours);
                         data[name] = model.noDecimalsWithSeparator(cost);
                     };

                          addEntry("interest", this.financialCost(current)),
                          addEntry("depreciation", this.depreciationCost(current)),
                          return data;
                 },




Monday, October 4, 2010
self = this
      attachFormListener: function(form, object) {
          var self = this;
          function populator(event) {
              self.populateFromForm(form, object);
              object.notify();
          };
          form.getElements().each(function(child) {
              child.observe('change', populator);
          });
      },




Monday, October 4, 2010
Mixins

                Object.mixin = function(destination, source) {
                   for (property in source)
                        destination[property] = source[property];
                   return destination;
                }




Monday, October 4, 2010
Test
                    • QUnit
                    • JsTest
                    • ScrewUnit
                    • jSpec
                    • ...
Monday, October 4, 2010
jQuery
                          write less, do more.




Monday, October 4, 2010
Monday, October 4, 2010
jQuery Philosophy


                    • Find some HTML
                    • Do something to it


Monday, October 4, 2010
Find some HTML
                          $(“div”)


                          <html>
                          <body>
                              <div>Malayan Tapir</div>
                              <div>Baird Tapir</div>
                          </body>
                          </html>



Monday, October 4, 2010
Do something to it!
                           $(“div”).addClass(‘cool’);


        <html>
        <body>
            <div class=‘cool’>Malayan Tapir</div>
            <div class=‘cool’>Baird Tapir</div>
        </body>
        </html>



Monday, October 4, 2010
Document Ready

                           $(function(){
                             // Document is ready
                           });




Monday, October 4, 2010
Tools




Monday, October 4, 2010
Debuggers

                    • Firebug
                    • Apple Dev Tools
                    • Chrome Dev Tools
                    • Internet Explorer Developer Tools

Monday, October 4, 2010
CSS Tools

                    • http://codylindley.com/jqueryselectors/
                    • Selector Gadget
                    • Nokogiri
                          •   XML, HTML, SAX Parser




Monday, October 4, 2010
Minification

                    • JsMin
                          •   http://www.crockford.com/javascript/
                              jsmin.html

                    • YUI Compressor
                          •   http://developer.yahoo.com/yui/compressor/




Monday, October 4, 2010
Build Tools

                    • Rake
                    • SCons
                    • Ant
                    • Scripts

Monday, October 4, 2010
File Watchers
                    • xrefresh for Firefox and IE
                          •   http://xrefresh.binaryage.com/

                    • LiveReload for Safari and Chrome
                          •   http://github.com/mockko/livereload

                    • Watchr
                          •   gem install watchr


Monday, October 4, 2010
Monday, October 4, 2010
Demo
                          Browser Command Line


                    • jQuery (10 lines)
                    • Sinatra (10 lines)
                    • LiveReload

Monday, October 4, 2010
Questions?
                             Anders Janmyr
                            http://anders.janmyr.com
                           anders.janmyr@jayway.com
                                @andersjanmyr




Monday, October 4, 2010

Más contenido relacionado

Destacado

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Javascript the Interlingua of the Web

  • 1. Javascript The Interlingua of the Web Anders Janmyr http://anders.janmyr.com anders.janmyr@jayway.com @andersjanmyr Monday, October 4, 2010
  • 2. Ugly Javascript Monday, October 4, 2010
  • 3. Beautiful Javascript Monday, October 4, 2010
  • 4. Y-Combinator Scheme (Lisp) (define Y (lambda (X) ((lambda (procedure) (X (lambda (arg) ((procedure procedure) arg)))) (lambda (procedure) (X (lambda (arg) ((procedure procedure) arg))))))) Monday, October 4, 2010
  • 5. Y-Combinator Javascript function Y (X) { return (function(procedure) { return X(function(arg) { return procedure(procedure)(arg); }); })(function(procedure) { return X(function(arg) { return procedure(procedure)(arg); }); }); } Monday, October 4, 2010
  • 6. Object Dynamic Monday, October 4, 2010
  • 7. Object Hashtable Monday, October 4, 2010
  • 8. Object Prototypical Monday, October 4, 2010
  • 9. Object Literal var empty_object = {}; Monday, October 4, 2010
  • 10. var kjell = { name: "Kjell", "kind": "Malayan" }; Monday, October 4, 2010
  • 11. var kjell = { name: "Kjell", "kind": "Malayan" address: { country: "Denmark" } }; Monday, October 4, 2010
  • 12. Retrieval kjell.name // “Kjell” kjell["name"] // “Kjell” // Denmark kjell.address.country kjell['address']["country"] Monday, October 4, 2010
  • 13. Retrieving non- existent properties kjell.firstname // undefined kjell["NAME"] // undefined kjell.home.country // Error Monday, October 4, 2010
  • 14. Setting non-existent properties kjell.firstname = 'Sven'; kjell["NAME"] = 'SVEN'; kjell.firstname // 'Sven' Monday, October 4, 2010
  • 15. Prototypical Inheritance using Object.create var rudolph = Object.create(kjell); rudolph.name // “Kjell” Monday, October 4, 2010
  • 16. Prototypical Inheritance name Kjell kind Malayan id a4r54ed prototype Monday, October 4, 2010
  • 17. rudolph.name = 'Rudolph'; rudolph.name // “Rudolph” kjell.name // “Kjell” rudolph.kind // ‘Malayan’ Monday, October 4, 2010
  • 18. rudolph.kind // ‘Malayan’ kjell.kind = 'Baird'; rudolph.kind // ‘Baird’ Monday, October 4, 2010
  • 19. delete rudolph.name rudolph.name // ‘Kjell’ Monday, October 4, 2010
  • 20. Prototypical Inheritance new Constructor(); Returns a new object with a link to Constructor.prototype Monday, October 4, 2010
  • 21. Prototypical Inheritance Object.create = function(o) { function F() {} F.prototype = o; return new F(); } Monday, October 4, 2010
  • 22. Arrays • Array inherits from object • Indexes are converted to strings • Magic length property • Always one larger than the highest int property Monday, October 4, 2010
  • 23. Array Literals •[] • names = [ ʻRudolphʼ, ʻKjellʼ, ʻTorstenʼ] • typeof value == ʻobjectʼ • value.constructor === Array Monday, October 4, 2010
  • 24. Array Methods • concat • join • pop • push • slice • sort • splice Monday, October 4, 2010
  • 25. JSON { "version": "1.0", "markets": [ { "name": "Europe", "currency": "EUR"}, { "name": "North America", "currency": "USD"}, { "name": "Other", "currency": "USD"} ], "serviceTypes": ["Maintenence", "WearingPart"], "valueTypes": ["Market value", "Trade in value"] } Monday, October 4, 2010
  • 26. Function First class object Monday, October 4, 2010
  • 27. Function lambda function() {}; Monday, October 4, 2010
  • 28. Function Statement function foo() {} expands to var foo = function foo() {}; Monday, October 4, 2010
  • 29. Functions Can be defined inside other functions! Monday, October 4, 2010
  • 30. residualValues: function(cur) { var self = this; return function() { return [1,2,3,4,5].map(function(age) { return { years: age, value: self.tradePercent(cur, age) }; }); } } Monday, October 4, 2010
  • 31. Methods functions stored in objects Monday, October 4, 2010
  • 32. Built-in Prototypes • Object.prototype • Array.prototype • Function.prototype • Number.prototype • String.prototype Monday, October 4, 2010
  • 33. Array.prototype.contains = function(element) { for (var i = 0; i < this.length; i++) { if (this[i] == element) return true; } return false; } [1, 2, 3].contains(3); // true Monday, October 4, 2010
  • 34. Function.prototype.method = function(name, func) { this.prototype[name] = func; return this; } Monday, October 4, 2010
  • 35. String.method(‘trim’, function() { return this.replace(/^s+|s+$/g, ‘’); } “ tapir “.trim(); // “tapir” Monday, October 4, 2010
  • 37. Scope The function is the scope of the variables Monday, October 4, 2010
  • 38. Invocation Forms • •Function form sleep(10) • •Method form kjell.sleep(10) • kjell[“sleep”](10) • Constructor form • new sleep(10) • •Apply form sleep.apply(rudolph, 10) Monday, October 4, 2010
  • 39. this Invocation this is an extra this Form dynamic the global parameter that function object depends on the method kjell calling form constructor a new object apply rudolph Monday, October 4, 2010
  • 40. arguments • A special array like, DYNAMIC, parameter • All the arguments of the invocation Monday, October 4, 2010
  • 41. function sum() { var i, n = arguments.length, total = 0; for (i = 0; i < n; i += 1) { total += arguments[i]; } return total; } sum(1, 2, 3, 4); Monday, October 4, 2010
  • 42. Dynamic Compilation • eval • Evaluates a string and returns the result. • new Function(parameterArray, codeString) • Creates and returns a function. • var add=new Function("a", "b", "return a+b;"); Monday, October 4, 2010
  • 43. Global Object • Container for all variables • On browsers window == global • Any var not declared is global • Global variables are Dangerous Monday, October 4, 2010
  • 44. Good Practices Monday, October 4, 2010
  • 45. Modules var MyNamespace = {}; var MyNS = MyNS || {}; Monday, October 4, 2010
  • 46. Cascade setFirst: function(name) { this.first = name; return this; } person .setFirst(“Anders”) .setLast(“Janmyr”) .setAge(40); Monday, October 4, 2010
  • 47. Encapsulation var Esperanto = Esperanto || {}; Esperanto.Lab = function() { var privVar = "example"; function privFunc() { return privVar; } return { example: function() { return privFunc(); } } }() Monday, October 4, 2010
  • 48. Local Functions costData: function(current) { var data = {}; function addEntry(name, cost) { data[name + "PerHour"] = model.withTwoDec(cost/hours); data[name] = model.noDecimalsWithSeparator(cost); }; addEntry("interest", this.financialCost(current)), addEntry("depreciation", this.depreciationCost(current)), return data; }, Monday, October 4, 2010
  • 49. self = this attachFormListener: function(form, object) { var self = this; function populator(event) { self.populateFromForm(form, object); object.notify(); }; form.getElements().each(function(child) { child.observe('change', populator); }); }, Monday, October 4, 2010
  • 50. Mixins Object.mixin = function(destination, source) { for (property in source) destination[property] = source[property]; return destination; } Monday, October 4, 2010
  • 51. Test • QUnit • JsTest • ScrewUnit • jSpec • ... Monday, October 4, 2010
  • 52. jQuery write less, do more. Monday, October 4, 2010
  • 54. jQuery Philosophy • Find some HTML • Do something to it Monday, October 4, 2010
  • 55. Find some HTML $(“div”) <html> <body> <div>Malayan Tapir</div> <div>Baird Tapir</div> </body> </html> Monday, October 4, 2010
  • 56. Do something to it! $(“div”).addClass(‘cool’); <html> <body> <div class=‘cool’>Malayan Tapir</div> <div class=‘cool’>Baird Tapir</div> </body> </html> Monday, October 4, 2010
  • 57. Document Ready $(function(){ // Document is ready }); Monday, October 4, 2010
  • 59. Debuggers • Firebug • Apple Dev Tools • Chrome Dev Tools • Internet Explorer Developer Tools Monday, October 4, 2010
  • 60. CSS Tools • http://codylindley.com/jqueryselectors/ • Selector Gadget • Nokogiri • XML, HTML, SAX Parser Monday, October 4, 2010
  • 61. Minification • JsMin • http://www.crockford.com/javascript/ jsmin.html • YUI Compressor • http://developer.yahoo.com/yui/compressor/ Monday, October 4, 2010
  • 62. Build Tools • Rake • SCons • Ant • Scripts Monday, October 4, 2010
  • 63. File Watchers • xrefresh for Firefox and IE • http://xrefresh.binaryage.com/ • LiveReload for Safari and Chrome • http://github.com/mockko/livereload • Watchr • gem install watchr Monday, October 4, 2010
  • 65. Demo Browser Command Line • jQuery (10 lines) • Sinatra (10 lines) • LiveReload Monday, October 4, 2010
  • 66. Questions? Anders Janmyr http://anders.janmyr.com anders.janmyr@jayway.com @andersjanmyr Monday, October 4, 2010