SlideShare a Scribd company logo
1 of 79
Download to read offline
{ The Beauty of JavaScript }
      Mike Girouard | AjaxWorld 2008
Hello.
I am a
Back-end guy in a front-end guy’s clothes
Sr. Developer at Magnani Caruso Dutton
JavaScript hacker since ’99
JavaScript developer/evangelist since ’07
Speaker: lovemikeg.com/talks
Blogger: lovemikeg.com/blog
I’d like to talk about a
language like no other.
JavaScript’s core is built on top of many
genius ideas.
JavaScript offers classless OOP.
JavaScript is a functional language.
JavaScript runs on the client-side &
server-side.
Many brilliant engineers have
contributed to JavaScript.
Unfortunately mouse trails left some
nasty scars.
Despite it’s aws, many
beautiful features exist.
JavaScript resembles C & Java.
foo.bar = ‘baz’;
if (foo < bar) {
    // do something
}
for (i = 0; i < n; i++) {
    // do something
}
while (i < n) {
    // do something
}
do {
       // something
}
while (i < n);
Everything is literal.
var name = ‘Mike G.’;
var age = 25;
var canDrink = true;
var colors = [‘red’, ‘green’, ‘blue’];
var hexColors = {
   ‘red’   : 0xFF0000,
   ‘green’ : 0x00FF00,
   ‘blue’ : 0x0000FF
};
var rgbColors = {
   ‘red’   : [255, 0, 0],
   ‘green’ : [0, 255, 0],
   ‘blue’ : [0, 0, 255]
};
Subscript notation is bad ass.
(foo.bar === foo[‘bar’])
var callbacks = {
  ‘#login-form’ : function () {
     // code to validate a login
  },

     ‘#print-btn’ : window.print
};
Functions are objects.
var foo = function () {
   return ++foo.counter;
};

foo.counter = 0;
JavaScript is functional.
var foo = function () {
   // do something
};
var id = function () {
   console.log(this.id);
};

lib.addEvent(foo, ‘click’, id);
lib.addEvent(bar, ‘click’, id);
mouseLib.rollOver(
   ‘some-element’,
   function () {
      this.src = ‘on.jpg’;
   },
   function () {
      this.src = ‘off.jpg’;
   }
);
(function () {

  // do something

})();
var outer, inner;

outer = function () {
    var counter = 0;

     inner = function () {
         return ++counter;
     };

     return counter;
};
Inheritance is achieved through
prototypes.
var Foo, Bar;

Foo = function () {};
Foo.prototype.bar = 123;

Bar = function () {};
Bar.prototype = new Foo;
Bar.prototype.bar = 456;
Don’t forget about Ajax.
var xhr;

xhr = new XMLHttpRequest;
xhr.onreadystatechange = function (event) {
    if (xhr.readyState === 4) {
        console.log(xhr.responseText);
    };
};

xhr.open(‘GET’,document.location.href,true);
xhr.send(null);
JavaScript is available of ine.
Expressive code breeds
beautiful patterns.
Self-invocation creates scope.
var foo = ‘bar’;
var baz = ‘bif’;
(function () {

  var foo = ‘bar’;
  var baz = ‘bif’;

})();
Load-time de nition/branching saves
trees.
var addEvent = (function () {
  if (window.addEventListener) {
    return addW3Event;
  }
  else if (window.attachEvent) {
    return addExplorerEvent;
  }
  else {
    return addLegacyEvent;
  }
})();
var getXHR = (function () {
  if (window.XMLHttpRequest) {
    return getW3XHR;
  }
  else if (window.ActiveXObject) {
    return getExplorerXHR;
  }
  else {
    throw ‘No XHR Support.’;
  }
})();
The Module enables private members.
var myLib = (function () {

  var $ = document.getElementById;
  var cache = {};

  return {
     getElement : function (id) {
       // do something
     }
  };

})();
Lazy function de nition saves even more
trees.
var getResource = function () {
    var resource, counter;

     
    resource = ‘foo’;
     
    counter = 0;
     
    getResource = function () {
     
        return resource +
          ‘ has been accessed ’ +
          (++counter) + ‘ times’;
    };
    return getResource();
};
Fragment templates create DOM nodes.
var getEmailTemplate = (function () {
    var email, link, check;

    email = document.createElement(‘div’);
    link = document.createElement(‘a’);
    check = document.createElement(‘input’);

    email.className = ‘email-item’;
    email.appendChild(check);
    email.appendChild(link);

    return function () {
        return email.cloneNode(true);
    };
})();
Node registries keep DOM nodes
organized.
var d     = document;
var byId = d.getElementById;
var byTag = d.getElementsByTagName;

var elements = {
   ‘foo’ : byId(‘foo’),
   ‘bar’ : byId(‘foo’).byTag(‘bar’)[0],
   ‘links’ : byTag(‘a’)
};
Libraries make beautiful
abstractions.
Prototype by Sam Stephenson.
$(…)
$$(…)   $A(…)   $F(…)

$H(…)   $R(…)   $w(…)
jQuery by John Resig
$(…)
$(‘#print’).click(function () {

  $(this).addClass(‘printed’);
  window.print();

});
YUI by Yahoo!
YAHOO
YAHOO.util.Dom
YAHOO.util.Event
YAHOO.namespace(‘mikeg’);
YAHOO.mikeg = (function () {

  var $ = YAHOO.util.Dom.get;
  var $$ = YAHOO.util.Selector.query;

  // do stuff

})();
It’s up to us to keep
JavaScript Beautiful.
Listen to Doug.
Pick a library, any library.
Protect the global object.
Use *Lint.
Use *Unit.
Educate others.
Be careful. JavaScript is not secure.
Thank you.
mikeg@lovemikeg.com
Oh yeah, we’re hiring!
careers@mcdpartners.com

More Related Content

What's hot

Resume - Shrikrishan - SOFTWARE TESTING
Resume - Shrikrishan - SOFTWARE TESTINGResume - Shrikrishan - SOFTWARE TESTING
Resume - Shrikrishan - SOFTWARE TESTING
shrikrishan upadhyay
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
洪 鹏发
 
Fajarul Akbar - Introduction & Installation to Flutter.pptx
Fajarul Akbar - Introduction & Installation to Flutter.pptxFajarul Akbar - Introduction & Installation to Flutter.pptx
Fajarul Akbar - Introduction & Installation to Flutter.pptx
IndahSariSitorus2MIM
 

What's hot (20)

AngularJS
AngularJS AngularJS
AngularJS
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...
Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...
Multiple Choice Questions on JAVA (object oriented programming) bank 2 -- bas...
 
Flutter workshop
Flutter workshopFlutter workshop
Flutter workshop
 
Resume - Shrikrishan - SOFTWARE TESTING
Resume - Shrikrishan - SOFTWARE TESTINGResume - Shrikrishan - SOFTWARE TESTING
Resume - Shrikrishan - SOFTWARE TESTING
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
Online event management system
Online event management systemOnline event management system
Online event management system
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
The SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design PatternsThe SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design Patterns
 
Android Thread
Android ThreadAndroid Thread
Android Thread
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
 
Selenium Automation Testing Interview Questions And Answers
Selenium Automation Testing Interview Questions And AnswersSelenium Automation Testing Interview Questions And Answers
Selenium Automation Testing Interview Questions And Answers
 
Selenium Testing Project report
Selenium Testing Project reportSelenium Testing Project report
Selenium Testing Project report
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Fajarul Akbar - Introduction & Installation to Flutter.pptx
Fajarul Akbar - Introduction & Installation to Flutter.pptxFajarul Akbar - Introduction & Installation to Flutter.pptx
Fajarul Akbar - Introduction & Installation to Flutter.pptx
 
ES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern JavascriptES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern Javascript
 
Java beans
Java beansJava beans
Java beans
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
 
ASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with OverviewASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with Overview
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
 

Similar to The Beauty of Java Script

The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5a
rajivmordani
 
Avinash Kundaliya: Javascript and WordPress
Avinash Kundaliya: Javascript and WordPressAvinash Kundaliya: Javascript and WordPress
Avinash Kundaliya: Javascript and WordPress
wpnepal
 
Orlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't SuckOrlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't Suck
erockendude
 
HTML5 APIs - Where no man has gone before! - Altran
HTML5 APIs - Where no man has gone before! - AltranHTML5 APIs - Where no man has gone before! - Altran
HTML5 APIs - Where no man has gone before! - Altran
Robert Nyman
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
Javascript Frameworks for Joomla
Javascript Frameworks for JoomlaJavascript Frameworks for Joomla
Javascript Frameworks for Joomla
Luke Summerfield
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajax
baygross
 
jQuery: out with the old, in with the new
jQuery: out with the old, in with the newjQuery: out with the old, in with the new
jQuery: out with the old, in with the new
Remy Sharp
 
Functional programming using underscorejs
Functional programming using underscorejsFunctional programming using underscorejs
Functional programming using underscorejs
偉格 高
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
David Padbury
 

Similar to The Beauty of Java Script (20)

The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5a
 
jQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyjQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journey
 
Avinash Kundaliya: Javascript and WordPress
Avinash Kundaliya: Javascript and WordPressAvinash Kundaliya: Javascript and WordPress
Avinash Kundaliya: Javascript and WordPress
 
Orlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't SuckOrlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't Suck
 
HTML5 APIs - Where no man has gone before! - Altran
HTML5 APIs - Where no man has gone before! - AltranHTML5 APIs - Where no man has gone before! - Altran
HTML5 APIs - Where no man has gone before! - Altran
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
jQuery: Events, Animation, Ajax
jQuery: Events, Animation, AjaxjQuery: Events, Animation, Ajax
jQuery: Events, Animation, Ajax
 
dojo.Patterns
dojo.Patternsdojo.Patterns
dojo.Patterns
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
 
JavaScript Core
JavaScript CoreJavaScript Core
JavaScript Core
 
Javascript Frameworks for Joomla
Javascript Frameworks for JoomlaJavascript Frameworks for Joomla
Javascript Frameworks for Joomla
 
How to actually use promises - Jakob Mattsson, FishBrain
How to actually use promises - Jakob Mattsson, FishBrainHow to actually use promises - Jakob Mattsson, FishBrain
How to actually use promises - Jakob Mattsson, FishBrain
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Secrets of JavaScript Libraries
Secrets of JavaScript LibrariesSecrets of JavaScript Libraries
Secrets of JavaScript Libraries
 
JavaScript - i och utanför webbläsaren (2010-03-03)
JavaScript - i och utanför webbläsaren (2010-03-03)JavaScript - i och utanför webbläsaren (2010-03-03)
JavaScript - i och utanför webbläsaren (2010-03-03)
 
Week 4 - jQuery + Ajax
Week 4 - jQuery + AjaxWeek 4 - jQuery + Ajax
Week 4 - jQuery + Ajax
 
jQuery: out with the old, in with the new
jQuery: out with the old, in with the newjQuery: out with the old, in with the new
jQuery: out with the old, in with the new
 
Functional programming using underscorejs
Functional programming using underscorejsFunctional programming using underscorejs
Functional programming using underscorejs
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 

More from Michael Girouard

JavaScript From Scratch: Writing Java Script Applications
JavaScript From Scratch: Writing Java Script ApplicationsJavaScript From Scratch: Writing Java Script Applications
JavaScript From Scratch: Writing Java Script Applications
Michael Girouard
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
Michael Girouard
 
Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5
Michael Girouard
 
Learning To Love Java Script
Learning To Love Java ScriptLearning To Love Java Script
Learning To Love Java Script
Michael Girouard
 
Learning To Love Java Script Color
Learning To Love Java Script ColorLearning To Love Java Script Color
Learning To Love Java Script Color
Michael Girouard
 

More from Michael Girouard (17)

Day to Day Realities of an Independent Worker
Day to Day Realities of an Independent WorkerDay to Day Realities of an Independent Worker
Day to Day Realities of an Independent Worker
 
Responsible JavaScript
Responsible JavaScriptResponsible JavaScript
Responsible JavaScript
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Ajax for PHP Developers
Ajax for PHP DevelopersAjax for PHP Developers
Ajax for PHP Developers
 
JavaScript From Scratch: Writing Java Script Applications
JavaScript From Scratch: Writing Java Script ApplicationsJavaScript From Scratch: Writing Java Script Applications
JavaScript From Scratch: Writing Java Script Applications
 
JavaScript From Scratch: Events
JavaScript From Scratch: EventsJavaScript From Scratch: Events
JavaScript From Scratch: Events
 
JavaScript From Scratch: Playing With Data
JavaScript From Scratch: Playing With DataJavaScript From Scratch: Playing With Data
JavaScript From Scratch: Playing With Data
 
JavaScript from Scratch: Getting Your Feet Wet
JavaScript from Scratch: Getting Your Feet WetJavaScript from Scratch: Getting Your Feet Wet
JavaScript from Scratch: Getting Your Feet Wet
 
Its More Than Just Markup
Its More Than Just MarkupIts More Than Just Markup
Its More Than Just Markup
 
Web Standards Evangelism
Web Standards EvangelismWeb Standards Evangelism
Web Standards Evangelism
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
 
A Look At Flex And Php
A Look At Flex And PhpA Look At Flex And Php
A Look At Flex And Php
 
Baking Cakes With Php
Baking Cakes With PhpBaking Cakes With Php
Baking Cakes With Php
 
Cfphp Zce 01 Basics
Cfphp Zce 01 BasicsCfphp Zce 01 Basics
Cfphp Zce 01 Basics
 
Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5
 
Learning To Love Java Script
Learning To Love Java ScriptLearning To Love Java Script
Learning To Love Java Script
 
Learning To Love Java Script Color
Learning To Love Java Script ColorLearning To Love Java Script Color
Learning To Love Java Script Color
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 

The Beauty of Java Script