JavaScript
Like a Box of Chocolates
“Are you telling me that I can’t get away
anymore without getting deeply into Javascript?”

                                         - Developer
“That is a disaster for me! I have made a career
out of actively avoiding Javascript.”

                                         - Developer
“If I cant continue to ignore Javascript, then you
may as well amputate one of my limbs.”

                                           - Developer
// Variable declaration
var firstName = "Forrest";

// Function declaration
function saying () {
    return "Stupid is as stupid does";
}
// If statement
if (badGrades) {
    return "Mom sleeps with teacher";
}

// Switch statement
var age = 10,
    lifeState;
switch (age) {
    case 10:
        lifeState = "Young";
        break;
    case 60:
        lifeState = "Old";
        break;
}
// Object literal
var forrest = {
    firstName : "Forrest"
};

// Array literal
var forrestFriends = ["Bubba", "Lieutenant Dan"];
// Shorthand assignment
function (boxOfChocolates) {
    var life = boxOfChocolates || "Snickers bar";
}

// Ternary operators
(looking)? "I gotta find Bubba!" : "It's ok";
// Short-circuit logic
if (obj && obj.property) {
    obj.property = "Lieutenant Dan, I got you some ice cream";
}
string
number
boolean
null
undefined
object
Array
string
           Date
number
           function
boolean
           RegExp
null
undefined
object
// Variable declaration
var firstName = "Forrest";

// Function declaration
function party () {
    return "Stupid is as stupid does";
}

typeof firstName // string
typeof party // function
// Object declaration
var forrest = {
    firstName : "Forrest"
};

// Array declaration
var forrestFriends = ["Bubba", "Lieutenant Dan"];



typeof forrest // object
typeof forrestFriends // object
// Object declaration
var forrest = {
    firstName : "Forrest"
};

// Array declaration
var forrestFriends = ["Bubba", "Lieutenant Dan"];


forrest forrest // object // false
 typeof instanceof Array
forrestFriends instanceof object // true
 typeof forrestFriends // Array
// Various "false" values
var nullVal = null;
var undefinedVal = undefined;
var zeroVal = 0;
var falseVal = false;
var emptyString = "";

// All would equal false in an if-clause
if (emptyString) {
    // Would never go in here
}
“Coercion is the practice of forcing another party
to behave in an involuntary manner”

                                          - Wikipedia
// Assignment
var happy = true;

// Equality
if (7 == "7") {
    // true
}

// Identity
if (7 === "7") {
    // false
}
// Assignment
                               rci on
                             oe
var happy = true;
                           c
// Equality        Ty pe
if (7 == "7") {
    // true
}

// Identity
if (7 === "7") {
    // false
}
// Type coercion
var sum = "5" + 6 + 7; // 567
// Prevent type coercion
var sum = parseInt("5", 10) + 6 + 7; // 18
// Self-invoking functions
(function () {
    var investment = "Lieutenant Dan got me
invested in some kind of fruit company.";
})();
// Using arguments
function friends (friend1, friend2) {
    return friend1 + " & " + friend2;
}

// Lieutenant Dan & Bubba
friends("Lieutenant Dan", "Bubba");

// Lieutenant Dan & undefined
friends("Lieutenant Dan");
// Using the arguments collection
function friends () {
    var allFriends = [];
    for (var i=0, il=arguments.length; i<il; i++) {
        allFriends.push(arguments[i]);
    };
    return allFriends.join(" & ");
}

// Lieutenant Dan & Bubba
friends("Lieutenant Dan", "Bubba");

// Lieutenant Dan
friends("Lieutenant Dan");
// Object declaration
function Forrest () {
    this.firstName = "Forrest";
    this.lastName = "Gump";
}

var forrest = new Forrest();
// Object declaration, literal style
var forrest = {
    firstName : "Forrest",
    lastName : "Gump"
};
// Iterating over properties
for (var item in forrest) {
     console.log(item + ": " + forrest[item]);
}
// Object declaration
var forrest = {
    firstName : "Forrest"
};

// Safe check for property
if ("firstName" in forrest) {
    console.log(forrest.firstName);
}
// Object declaration
function ForrestAsChild {
    this.firstName = "Forrest";
};

// Method set via prototype
ForrestAsChild.prototype.runsFast = function () {
    return true;
};
// Object declaration
function ForrestAsGrownup {
    this.joinsArmy = true;
};

// Prototype for new object
ForrestAsGrownup.prototype = new ForrestAsChild;

// Method set via prototype
ForrestAsGrownup.prototype.ruinsBathrobe = function () {
    return "I think I ruined your roommate's bathrobe";
};
// Create an instance
var forrest = new ForrestAsGrownup();

// Returns "I think I ruined your roommate's bathrobe"
forrest.ruinsBathrobe();

// Returns true - from ForrestAsChild
forrest.runsFast();

// Fails
forrest.codesJavaScript();
forrest instance

ForrestAsGrownup

ForrestAsChild

Object
// Extending core JavaScript objects
if (typeof Array.prototype.push === "undefined") {
    Array.prototype.push = function () {
        for (var i=0, il=arguments.length; i<il; i++) {
            this[this.length] = arguments[i];
        };
        return this;
    }
}



var locations = ["Vietnam"];
locations.push("China", "White House");
// locations = ["Vietnam", "China", "White House"];
// Scope - global or local

// Global
var quote = "I had run for 3 years, 2 months,
14 days, and 16 hours."

function () {
    // Local
    var pantherParty = "I'm sorry I had to
fight in the middle of your Black Panther
party.";

    // Global
    question = "And so, you just ran?";
}
// Global
function meetingJFK () {
    var JFKQuestion = "Congratulations, how do
you feel?";

    // Local
    function forrestReply () {
        return "I gotta pee.";
    }

    return forrestReply();
}

meetingJFK(); // I gotta pee
forrestReply(); // Error: not accessible
// Controlling scope
function whoAmI () {
    return this.nodeName;
}

whoAmI(); // undefined
whoAmI.call(document, "Hello"); // #document
whoAmI.apply(document.body, ["Hello", "Greetings?"]); // BODY
// closures
function happens (what) {
    return function (verb) {
        return what + " " + verb;
    }
}

var action = happens("Shit");

action("happens"); // Shit happens
// closures
function happens (what) {
    return function (verb) {
        return what + " " + verb;
    }
}

var action = happens("Shit");

// Breaking it down
var action = function (verb) {
    return "Shit" + " " + verb;
};
// closures
function happens (what) {
    return function (verb) {
        return what + " " + verb;
    }
}

var action = happens("Shit");

// Breaking it down
var action = function (verb) {
    return "Shit" + " " + verb;
};
var link;
for (var i=0; i<3; i++) {
    link = document.createElement("a");
    link.innerHTML = "Link " + i;
    link.onclick = function () {
        alert("I am link " + i);
    };
    document.body.appendChild(link);
};
var link;
for (var i=0; i<3; i++) {
    link = document.createElement("a");
    link.innerHTML = "Link " + i;
    link.onclick = function (index) {
        return function () {
            alert("I am link " + index);
        };
    }(i);
    document.body.appendChild(link);
};
var link;
for (var i=0; i<3; i++) {
    link = document.createElement("a");
    link.innerHTML = "Link " + i;
    link.onclick = function (index) {
        return function () {
            alert("I am link " + index);
        };
    }(i);
    document.body.appendChild(link);
};
var link;
for (var i=0; i<3; i++) {
    link = document.createElement("a");
    link.innerHTML = "Link " + i;
    link.onclick = function (index) {
        return function () {
            alert("I am link " + index);
        };
    }(i);
    document.body.appendChild(link);
};
var link;
for (var i=0; i<3; i++) {
    link = document.createElement("a");
    link.innerHTML = "Link " + i;
    link.onclick = function (index) {
        return function () {
            alert("I am link " + index);
        };
    }(i);
    document.body.appendChild(link);
};
// Yahoo! JavaScript Module Pattern
var forrest = function () {
    var firstName = "Forrest";

       return {
           getFirstName : function () {
                return firstName;
           }
       };
}();

// Returns "Forrest"
forrest.getFirstName();
// Yahoo! JavaScript Module Pattern
var forrest = function () {
    var firstName = "Forrest",
        getFirstName = function () {
            return firstName;
        };

       return {
           getFirstName : getFirstName
       };
}();

// Returns "Forrest"
forrest.getFirstName();
// Namespacing
var Movie = {};

// Yahoo! JavaScript Module Pattern
Movie.forrest = function () {
    var lastName = "Gump";

       return {
           firstName : "Forrest",
           getFirstName : function () {
                return this.firstName;
           }
       };
}();
// Yahoo! JavaScript Module Pattern
Movie.forrest = function () {
    var lastName = "Gump";

       return {
           firstName : "Forrest",
           getFirstName : function () {
                return this.firstName;
           }
       };
}();



// Yahoo! JavaScript Module Pattern
Movie.lieutenantDan = function () {
    var lastName = "Taylor";

       return {
           firstName : "Dan",
           getFullName : function () {
                return Movie.forrest.getFirstName.call(this) + " " + lastName;
           }
       };
}();

Movie.lieutenantDan.getFullName();
// Yahoo! JavaScript Module Pattern
Movie.forrest = function () {
    var lastName = "Gump";

       return {
           firstName : "Forrest",
           getFirstName : function () {
                return this.firstName;
           }
       };
}();



// Yahoo! JavaScript Module Pattern
Movie.lieutenantDan = function () {
    var lastName = "Taylor";

       return {
           firstName : "Dan",
           getFullName : function () {
                return Movie.forrest.getFirstName.call(this) + " " + lastName;
           }
       };
}();

Movie.lieutenantDan.getFullName();
// Minimize DOM access
document.getElementById("container").className = "js-enabled";
document.getElementById("container").innerHTML += "Hello Amsterdam";
document.getElementById("container").innerHTML += "Tell me how you doin'!";
document.getElementById("container").innerHTML += "I went on a nice boat
                                                   ride last night!";
document.getElementById("container").innerHTML += "...where Carrot Top made
                                                   a pass at me...";
// Minimize DOM access
var container = document.getElementById("container"),
    content = "Hello Amsterdam";
container.className = "js-enabled";
content += "Tell me how you doin'!";
content += "I went on a nice boat ride last night!";
content += "...where Carrot Top made a pass at me...";
container.innerHTML = content;
// Minimize DOM access
var container = document.getElementById("container"),
    content = "Hello Amsterdam";
container.className = "js-enabled";
content += "Tell me how you doin'!";
content += "I went on a nice boat ride last night!";
content += "...where Carrot Top made a pass at me...";
container.innerHTML = content;
// Variable declaration
function richAndStupid () {
    var rich = "And cause I was a gazillionaire, I
cut that grass for free.",
        stupid = "Stupid is as stupid does.";
}
// Looping, variables and array lookups
function forrestForm () {
    var allParagraphs = document.getElementsByTagName("p");
    for (var i=0; i<allParagraphs.length; i++) {
        var link = document.createElement("a");
        link.href = "http://en.wikipedia.org/wiki/Forrest_Gump";
        link.title = "Read about Forrest Gump at Wikipedia";
        link.innerHTML = "Forrest Gump";

        allParagraphs[i].className = "Forrested";
        allParagraphs[i].appendChild(link);
    }
}
// Looping, variables and array lookups
function forrestForm () {
    var allParagraphs = document.getElementsByTagName("p");
    for (var i=0; i<allParagraphs.length; i++) {
        var link = document.createElement("a");
        link.href = "http://en.wikipedia.org/wiki/Forrest_Gump";
        link.title = "Read about Forrest Gump at Wikipedia";
        link.innerHTML = "Forrest Gump";

        allParagraphs[i].className = "Forrested";
        allParagraphs[i].appendChild(link);
    }
}
// Looping, variables and array lookups
function forrestForm () {
    var allParagraphs = document.getElementsByTagName("p");
    for (var i=0; i<allParagraphs.length; i++) {
        var link = document.createElement("a");
        link.href = "http://en.wikipedia.org/wiki/Forrest_Gump";
        link.title = "Read about Forrest Gump at Wikipedia";
        link.innerHTML = "Forrest Gump";

        allParagraphs[i].className = "Forrested";
        allParagraphs[i].appendChild(link);
    }
}
// Looping, variables and array lookups
function forrestForm () {
    var allParagraphs = document.getElementsByTagName("p");
    for (var i=0; i<allParagraphs.length; i++) {
        var link = document.createElement("a");
        link.href = "http://en.wikipedia.org/wiki/Forrest_Gump";
        link.title = "Read about Forrest Gump at Wikipedia";
        link.innerHTML = "Forrest Gump";

        allParagraphs[i].className = "Forrested";
        allParagraphs[i].appendChild(link);
    }
}
// Looping, variables and array lookups
function forrestForm () {
    var allParagraphs = document.getElementsByTagName("p");
    for (var i=0, l=allParagraphs.length, link, paragraph; i<l; i++) {
        link = document.createElement("a");
        link.href = "http://en.wikipedia.org/wiki/Forrest_Gump";
        link.title = "Read about Forrest Gump at Wikipedia";
        link.innerHTML = "Forrest Gump";

        paragraph = allParagraphs[i];
        paragraph.className = "Forrested";
        paragraph.appendChild(link);
    }
}
// Looping, variables and array lookups
function forrestForm () {
    var allParagraphs = document.getElementsByTagName("p");
    for (var i=0, l=allParagraphs.length, link, paragraph; i<l; i++) {
        link = document.createElement("a");
        link.href = "http://en.wikipedia.org/wiki/Forrest_Gump";
        link.title = "Read about Forrest Gump at Wikipedia";
        link.innerHTML = "Forrest Gump";

        paragraph = allParagraphs[i];
        paragraph.className = "Forrested";
        paragraph.appendChild(link);
    }
}
// Looping, variables and array lookups
function forrestForm () {
    var allParagraphs = document.getElementsByTagName("p");
    for (var i=0, l=allParagraphs.length, link, paragraph; i<l; i++) {
        link = document.createElement("a");
        link.href = "http://en.wikipedia.org/wiki/Forrest_Gump";
        link.title = "Read about Forrest Gump at Wikipedia";
        link.innerHTML = "Forrest Gump";

        paragraph = allParagraphs[i];
        paragraph.className = "Forrested";
        paragraph.appendChild(link);
    }
}
// Looping, variables and array lookups
function forrestForm () {
    var allParagraphs = document.getElementsByTagName("p");
    for (var i=0, l=allParagraphs.length, link, paragraph; i<l; i++) {
        link = document.createElement("a");
        link.href = "http://en.wikipedia.org/wiki/Forrest_Gump";
        link.title = "Read about Forrest Gump at Wikipedia";
        link.innerHTML = "Forrest Gump";

        paragraph = allParagraphs[i];
        paragraph.className = "Forrested";
        paragraph.appendChild(link);
    }
}
// Semicolon insertion
return
{
    javascript : "Fantastic!"
};
// Semicolon insertion
return; // Semicolon insertion
{ // Considered an empty block
    javascript : "Fantastic!"
}; // Semicolon insertion, dummy line
JSLint
Robert Nyman
                                                              http://robertnyman.com/speaking/

                                                                                Twitter: @robertnyman



Pictures:

Ninja Turtle: http://www.originalprop.com/blog/2008/03/20/teenage-mutant-ninja-turtles-costume-restoration/     Dog (Cat): http://www.cartoonstock.com/directory/f/false_identity.asp
Bruce Willis: http://www.starsjournal.com/3192/bruce-willis-is-being-sued-for-4-million-dollars.html            Hillary Clinton & Soldier: http://confederateyankee.mu.nu/archives/154032.php
Swedish flag: http://www.olssonfoto.se/JAlbum/SGP%202008%20Ullevi/slides/Svenska%20flaggan.html                   Play with yourself: http://www.justwhatshesaid.com/?p=965
Euro Coin: http://accidentaldong.blogspot.com/2009/10/euro-uh-oh.html                                           Overloading: http://theshadowhive.blogspot.com/2010/04/mutating-chaos-gene.html
                                                                                                                Brad Pitt: http://open.salon.com/blog/just-walt/2009/10/29/real_men_he-men_pictures_whos_the_manliest_of_men
                                                                                                                Kristen Bell: http://veronica-mars-episodes.download-tvshows.com/kristen-bell-loves-megan-fox/
Most popular language: http://odetocode.com/Blogs/scott/archive/2009/03/18/signs-that-your-javascript-skills-   Extensible table: http://www.amishshowroom.com/index.php?main_page=index&cPath=40_64
need-updating.aspx                                                                                              Tiger Woods: http://blogs.bigadda.com/pal4868546/2010/01/
Sunrise: http://www.manywallpapers.com/space-wallpapers/earth/sunrise-from-space.html                           Pollution: http://blog.lib.umn.edu/cramb005/architecture/
Astronaut: http://martianchronicles.wordpress.com/2009/01/23/carnival-of-space-87/                              Closure: http://today.msnbc.msn.com/id/4760120
Netscape 2: http://blog.explorelearning.com/2005/12/index.html                                                  Steve Ballmer: http://www.businessinsider.com/microsoft-completely-rebooted-its-mobile-strategy-yesterday-heres-
Internet Explorer 3: http://www.guidebookgallery.org/screenshots/browser                                        what-you-missed-2010-2
Gandalf: http://web.mit.edu/kayla/Public/Backgrounds/LOTR%20Gandalf%204.JPG                                     Inheritance: http://tithebarn.wordpress.com/2010/04/26/the-meek-shall-inherit-if-thats-alright-with-the-rest-of-you/
Now: http://www.geekologie.com/2007/07/15-week/                                                                 Crockford: http://gemsres.com/story/nov07/468365/Crockford_New_4681.jpg
Axe: http://bestgamewallpapers.com/a3-the-age-of-sovereign/axe                                                  Name: http://blog.usa.gov/roller/govgab/tags/names
Time: http://www.mindhacks.com/blog/seeing/index.html                                                           Space: http://gucken.deviantart.com/art/Sunrise-in-Space-56420137
Money: http://www.mediabistro.com/unbeige/ideas/                                                                Fail better: http://ozguralaz.posterous.com/ever-tried-ever-failed-no-matt
Happy Ape: http://thesituationist.wordpress.com/2007/06/14/
High speed train: http://www.freefoto.com/preview/23-22-1?ffid=23-22-1                                           Mila & Macaulay: http://uk.eonline.com/uberblog/b61889_mila_macaulay_home_alone.html
Sunspider results: http://ie.microsoft.com/testdrive/benchmarks/sunspider/default.html                          Hearts: http://www.funonthenet.in/content/view/395/31/
Forrest Gump: http://wallpaper-s.org/36__Forrest_Gump,_1994,_Tom_Hanks,_Robin_Wright_Penn.htm
Data: http://walrus.wr.usgs.gov/infobank/programs/html/definition/datadictionary.html
JavaScript - Like a Box of Chocolates

JavaScript - Like a Box of Chocolates

  • 1.
    JavaScript Like a Boxof Chocolates
  • 15.
    “Are you tellingme that I can’t get away anymore without getting deeply into Javascript?” - Developer
  • 16.
    “That is adisaster for me! I have made a career out of actively avoiding Javascript.” - Developer
  • 17.
    “If I cantcontinue to ignore Javascript, then you may as well amputate one of my limbs.” - Developer
  • 24.
    // Variable declaration varfirstName = "Forrest"; // Function declaration function saying () { return "Stupid is as stupid does"; }
  • 25.
    // If statement if(badGrades) { return "Mom sleeps with teacher"; } // Switch statement var age = 10, lifeState; switch (age) { case 10: lifeState = "Young"; break; case 60: lifeState = "Old"; break; }
  • 26.
    // Object literal varforrest = { firstName : "Forrest" }; // Array literal var forrestFriends = ["Bubba", "Lieutenant Dan"];
  • 27.
    // Shorthand assignment function(boxOfChocolates) { var life = boxOfChocolates || "Snickers bar"; } // Ternary operators (looking)? "I gotta find Bubba!" : "It's ok";
  • 28.
    // Short-circuit logic if(obj && obj.property) { obj.property = "Lieutenant Dan, I got you some ice cream"; }
  • 30.
  • 31.
    Array string Date number function boolean RegExp null undefined object
  • 32.
    // Variable declaration varfirstName = "Forrest"; // Function declaration function party () { return "Stupid is as stupid does"; } typeof firstName // string typeof party // function
  • 33.
    // Object declaration varforrest = { firstName : "Forrest" }; // Array declaration var forrestFriends = ["Bubba", "Lieutenant Dan"]; typeof forrest // object typeof forrestFriends // object
  • 34.
    // Object declaration varforrest = { firstName : "Forrest" }; // Array declaration var forrestFriends = ["Bubba", "Lieutenant Dan"]; forrest forrest // object // false typeof instanceof Array forrestFriends instanceof object // true typeof forrestFriends // Array
  • 36.
    // Various "false"values var nullVal = null; var undefinedVal = undefined; var zeroVal = 0; var falseVal = false; var emptyString = ""; // All would equal false in an if-clause if (emptyString) { // Would never go in here }
  • 38.
    “Coercion is thepractice of forcing another party to behave in an involuntary manner” - Wikipedia
  • 39.
    // Assignment var happy= true; // Equality if (7 == "7") { // true } // Identity if (7 === "7") { // false }
  • 40.
    // Assignment rci on oe var happy = true; c // Equality Ty pe if (7 == "7") { // true } // Identity if (7 === "7") { // false }
  • 41.
    // Type coercion varsum = "5" + 6 + 7; // 567
  • 42.
    // Prevent typecoercion var sum = parseInt("5", 10) + 6 + 7; // 18
  • 44.
    // Self-invoking functions (function() { var investment = "Lieutenant Dan got me invested in some kind of fruit company."; })();
  • 46.
    // Using arguments functionfriends (friend1, friend2) { return friend1 + " & " + friend2; } // Lieutenant Dan & Bubba friends("Lieutenant Dan", "Bubba"); // Lieutenant Dan & undefined friends("Lieutenant Dan");
  • 47.
    // Using thearguments collection function friends () { var allFriends = []; for (var i=0, il=arguments.length; i<il; i++) { allFriends.push(arguments[i]); }; return allFriends.join(" & "); } // Lieutenant Dan & Bubba friends("Lieutenant Dan", "Bubba"); // Lieutenant Dan friends("Lieutenant Dan");
  • 49.
    // Object declaration functionForrest () { this.firstName = "Forrest"; this.lastName = "Gump"; } var forrest = new Forrest();
  • 50.
    // Object declaration,literal style var forrest = { firstName : "Forrest", lastName : "Gump" };
  • 51.
    // Iterating overproperties for (var item in forrest) { console.log(item + ": " + forrest[item]); }
  • 52.
    // Object declaration varforrest = { firstName : "Forrest" }; // Safe check for property if ("firstName" in forrest) { console.log(forrest.firstName); }
  • 54.
    // Object declaration functionForrestAsChild { this.firstName = "Forrest"; }; // Method set via prototype ForrestAsChild.prototype.runsFast = function () { return true; };
  • 55.
    // Object declaration functionForrestAsGrownup { this.joinsArmy = true; }; // Prototype for new object ForrestAsGrownup.prototype = new ForrestAsChild; // Method set via prototype ForrestAsGrownup.prototype.ruinsBathrobe = function () { return "I think I ruined your roommate's bathrobe"; };
  • 56.
    // Create aninstance var forrest = new ForrestAsGrownup(); // Returns "I think I ruined your roommate's bathrobe" forrest.ruinsBathrobe(); // Returns true - from ForrestAsChild forrest.runsFast(); // Fails forrest.codesJavaScript();
  • 57.
  • 59.
    // Extending coreJavaScript objects if (typeof Array.prototype.push === "undefined") { Array.prototype.push = function () { for (var i=0, il=arguments.length; i<il; i++) { this[this.length] = arguments[i]; }; return this; } } var locations = ["Vietnam"]; locations.push("China", "White House"); // locations = ["Vietnam", "China", "White House"];
  • 61.
    // Scope -global or local // Global var quote = "I had run for 3 years, 2 months, 14 days, and 16 hours." function () { // Local var pantherParty = "I'm sorry I had to fight in the middle of your Black Panther party."; // Global question = "And so, you just ran?"; }
  • 62.
    // Global function meetingJFK() { var JFKQuestion = "Congratulations, how do you feel?"; // Local function forrestReply () { return "I gotta pee."; } return forrestReply(); } meetingJFK(); // I gotta pee forrestReply(); // Error: not accessible
  • 63.
    // Controlling scope functionwhoAmI () { return this.nodeName; } whoAmI(); // undefined whoAmI.call(document, "Hello"); // #document whoAmI.apply(document.body, ["Hello", "Greetings?"]); // BODY
  • 66.
    // closures function happens(what) { return function (verb) { return what + " " + verb; } } var action = happens("Shit"); action("happens"); // Shit happens
  • 68.
    // closures function happens(what) { return function (verb) { return what + " " + verb; } } var action = happens("Shit"); // Breaking it down var action = function (verb) { return "Shit" + " " + verb; };
  • 69.
    // closures function happens(what) { return function (verb) { return what + " " + verb; } } var action = happens("Shit"); // Breaking it down var action = function (verb) { return "Shit" + " " + verb; };
  • 70.
    var link; for (vari=0; i<3; i++) { link = document.createElement("a"); link.innerHTML = "Link " + i; link.onclick = function () { alert("I am link " + i); }; document.body.appendChild(link); };
  • 71.
    var link; for (vari=0; i<3; i++) { link = document.createElement("a"); link.innerHTML = "Link " + i; link.onclick = function (index) { return function () { alert("I am link " + index); }; }(i); document.body.appendChild(link); };
  • 72.
    var link; for (vari=0; i<3; i++) { link = document.createElement("a"); link.innerHTML = "Link " + i; link.onclick = function (index) { return function () { alert("I am link " + index); }; }(i); document.body.appendChild(link); };
  • 73.
    var link; for (vari=0; i<3; i++) { link = document.createElement("a"); link.innerHTML = "Link " + i; link.onclick = function (index) { return function () { alert("I am link " + index); }; }(i); document.body.appendChild(link); };
  • 74.
    var link; for (vari=0; i<3; i++) { link = document.createElement("a"); link.innerHTML = "Link " + i; link.onclick = function (index) { return function () { alert("I am link " + index); }; }(i); document.body.appendChild(link); };
  • 76.
    // Yahoo! JavaScriptModule Pattern var forrest = function () { var firstName = "Forrest"; return { getFirstName : function () { return firstName; } }; }(); // Returns "Forrest" forrest.getFirstName();
  • 77.
    // Yahoo! JavaScriptModule Pattern var forrest = function () { var firstName = "Forrest", getFirstName = function () { return firstName; }; return { getFirstName : getFirstName }; }(); // Returns "Forrest" forrest.getFirstName();
  • 79.
    // Namespacing var Movie= {}; // Yahoo! JavaScript Module Pattern Movie.forrest = function () { var lastName = "Gump"; return { firstName : "Forrest", getFirstName : function () { return this.firstName; } }; }();
  • 80.
    // Yahoo! JavaScriptModule Pattern Movie.forrest = function () { var lastName = "Gump"; return { firstName : "Forrest", getFirstName : function () { return this.firstName; } }; }(); // Yahoo! JavaScript Module Pattern Movie.lieutenantDan = function () { var lastName = "Taylor"; return { firstName : "Dan", getFullName : function () { return Movie.forrest.getFirstName.call(this) + " " + lastName; } }; }(); Movie.lieutenantDan.getFullName();
  • 81.
    // Yahoo! JavaScriptModule Pattern Movie.forrest = function () { var lastName = "Gump"; return { firstName : "Forrest", getFirstName : function () { return this.firstName; } }; }(); // Yahoo! JavaScript Module Pattern Movie.lieutenantDan = function () { var lastName = "Taylor"; return { firstName : "Dan", getFullName : function () { return Movie.forrest.getFirstName.call(this) + " " + lastName; } }; }(); Movie.lieutenantDan.getFullName();
  • 83.
    // Minimize DOMaccess document.getElementById("container").className = "js-enabled"; document.getElementById("container").innerHTML += "Hello Amsterdam"; document.getElementById("container").innerHTML += "Tell me how you doin'!"; document.getElementById("container").innerHTML += "I went on a nice boat ride last night!"; document.getElementById("container").innerHTML += "...where Carrot Top made a pass at me...";
  • 84.
    // Minimize DOMaccess var container = document.getElementById("container"), content = "Hello Amsterdam"; container.className = "js-enabled"; content += "Tell me how you doin'!"; content += "I went on a nice boat ride last night!"; content += "...where Carrot Top made a pass at me..."; container.innerHTML = content;
  • 85.
    // Minimize DOMaccess var container = document.getElementById("container"), content = "Hello Amsterdam"; container.className = "js-enabled"; content += "Tell me how you doin'!"; content += "I went on a nice boat ride last night!"; content += "...where Carrot Top made a pass at me..."; container.innerHTML = content;
  • 86.
    // Variable declaration functionrichAndStupid () { var rich = "And cause I was a gazillionaire, I cut that grass for free.", stupid = "Stupid is as stupid does."; }
  • 87.
    // Looping, variablesand array lookups function forrestForm () { var allParagraphs = document.getElementsByTagName("p"); for (var i=0; i<allParagraphs.length; i++) { var link = document.createElement("a"); link.href = "http://en.wikipedia.org/wiki/Forrest_Gump"; link.title = "Read about Forrest Gump at Wikipedia"; link.innerHTML = "Forrest Gump"; allParagraphs[i].className = "Forrested"; allParagraphs[i].appendChild(link); } }
  • 88.
    // Looping, variablesand array lookups function forrestForm () { var allParagraphs = document.getElementsByTagName("p"); for (var i=0; i<allParagraphs.length; i++) { var link = document.createElement("a"); link.href = "http://en.wikipedia.org/wiki/Forrest_Gump"; link.title = "Read about Forrest Gump at Wikipedia"; link.innerHTML = "Forrest Gump"; allParagraphs[i].className = "Forrested"; allParagraphs[i].appendChild(link); } }
  • 89.
    // Looping, variablesand array lookups function forrestForm () { var allParagraphs = document.getElementsByTagName("p"); for (var i=0; i<allParagraphs.length; i++) { var link = document.createElement("a"); link.href = "http://en.wikipedia.org/wiki/Forrest_Gump"; link.title = "Read about Forrest Gump at Wikipedia"; link.innerHTML = "Forrest Gump"; allParagraphs[i].className = "Forrested"; allParagraphs[i].appendChild(link); } }
  • 90.
    // Looping, variablesand array lookups function forrestForm () { var allParagraphs = document.getElementsByTagName("p"); for (var i=0; i<allParagraphs.length; i++) { var link = document.createElement("a"); link.href = "http://en.wikipedia.org/wiki/Forrest_Gump"; link.title = "Read about Forrest Gump at Wikipedia"; link.innerHTML = "Forrest Gump"; allParagraphs[i].className = "Forrested"; allParagraphs[i].appendChild(link); } }
  • 91.
    // Looping, variablesand array lookups function forrestForm () { var allParagraphs = document.getElementsByTagName("p"); for (var i=0, l=allParagraphs.length, link, paragraph; i<l; i++) { link = document.createElement("a"); link.href = "http://en.wikipedia.org/wiki/Forrest_Gump"; link.title = "Read about Forrest Gump at Wikipedia"; link.innerHTML = "Forrest Gump"; paragraph = allParagraphs[i]; paragraph.className = "Forrested"; paragraph.appendChild(link); } }
  • 92.
    // Looping, variablesand array lookups function forrestForm () { var allParagraphs = document.getElementsByTagName("p"); for (var i=0, l=allParagraphs.length, link, paragraph; i<l; i++) { link = document.createElement("a"); link.href = "http://en.wikipedia.org/wiki/Forrest_Gump"; link.title = "Read about Forrest Gump at Wikipedia"; link.innerHTML = "Forrest Gump"; paragraph = allParagraphs[i]; paragraph.className = "Forrested"; paragraph.appendChild(link); } }
  • 93.
    // Looping, variablesand array lookups function forrestForm () { var allParagraphs = document.getElementsByTagName("p"); for (var i=0, l=allParagraphs.length, link, paragraph; i<l; i++) { link = document.createElement("a"); link.href = "http://en.wikipedia.org/wiki/Forrest_Gump"; link.title = "Read about Forrest Gump at Wikipedia"; link.innerHTML = "Forrest Gump"; paragraph = allParagraphs[i]; paragraph.className = "Forrested"; paragraph.appendChild(link); } }
  • 94.
    // Looping, variablesand array lookups function forrestForm () { var allParagraphs = document.getElementsByTagName("p"); for (var i=0, l=allParagraphs.length, link, paragraph; i<l; i++) { link = document.createElement("a"); link.href = "http://en.wikipedia.org/wiki/Forrest_Gump"; link.title = "Read about Forrest Gump at Wikipedia"; link.innerHTML = "Forrest Gump"; paragraph = allParagraphs[i]; paragraph.className = "Forrested"; paragraph.appendChild(link); } }
  • 95.
    // Semicolon insertion return { javascript : "Fantastic!" };
  • 96.
    // Semicolon insertion return;// Semicolon insertion { // Considered an empty block javascript : "Fantastic!" }; // Semicolon insertion, dummy line
  • 97.
  • 99.
    Robert Nyman http://robertnyman.com/speaking/ Twitter: @robertnyman Pictures: Ninja Turtle: http://www.originalprop.com/blog/2008/03/20/teenage-mutant-ninja-turtles-costume-restoration/ Dog (Cat): http://www.cartoonstock.com/directory/f/false_identity.asp Bruce Willis: http://www.starsjournal.com/3192/bruce-willis-is-being-sued-for-4-million-dollars.html Hillary Clinton & Soldier: http://confederateyankee.mu.nu/archives/154032.php Swedish flag: http://www.olssonfoto.se/JAlbum/SGP%202008%20Ullevi/slides/Svenska%20flaggan.html Play with yourself: http://www.justwhatshesaid.com/?p=965 Euro Coin: http://accidentaldong.blogspot.com/2009/10/euro-uh-oh.html Overloading: http://theshadowhive.blogspot.com/2010/04/mutating-chaos-gene.html Brad Pitt: http://open.salon.com/blog/just-walt/2009/10/29/real_men_he-men_pictures_whos_the_manliest_of_men Kristen Bell: http://veronica-mars-episodes.download-tvshows.com/kristen-bell-loves-megan-fox/ Most popular language: http://odetocode.com/Blogs/scott/archive/2009/03/18/signs-that-your-javascript-skills- Extensible table: http://www.amishshowroom.com/index.php?main_page=index&cPath=40_64 need-updating.aspx Tiger Woods: http://blogs.bigadda.com/pal4868546/2010/01/ Sunrise: http://www.manywallpapers.com/space-wallpapers/earth/sunrise-from-space.html Pollution: http://blog.lib.umn.edu/cramb005/architecture/ Astronaut: http://martianchronicles.wordpress.com/2009/01/23/carnival-of-space-87/ Closure: http://today.msnbc.msn.com/id/4760120 Netscape 2: http://blog.explorelearning.com/2005/12/index.html Steve Ballmer: http://www.businessinsider.com/microsoft-completely-rebooted-its-mobile-strategy-yesterday-heres- Internet Explorer 3: http://www.guidebookgallery.org/screenshots/browser what-you-missed-2010-2 Gandalf: http://web.mit.edu/kayla/Public/Backgrounds/LOTR%20Gandalf%204.JPG Inheritance: http://tithebarn.wordpress.com/2010/04/26/the-meek-shall-inherit-if-thats-alright-with-the-rest-of-you/ Now: http://www.geekologie.com/2007/07/15-week/ Crockford: http://gemsres.com/story/nov07/468365/Crockford_New_4681.jpg Axe: http://bestgamewallpapers.com/a3-the-age-of-sovereign/axe Name: http://blog.usa.gov/roller/govgab/tags/names Time: http://www.mindhacks.com/blog/seeing/index.html Space: http://gucken.deviantart.com/art/Sunrise-in-Space-56420137 Money: http://www.mediabistro.com/unbeige/ideas/ Fail better: http://ozguralaz.posterous.com/ever-tried-ever-failed-no-matt Happy Ape: http://thesituationist.wordpress.com/2007/06/14/ High speed train: http://www.freefoto.com/preview/23-22-1?ffid=23-22-1 Mila & Macaulay: http://uk.eonline.com/uberblog/b61889_mila_macaulay_home_alone.html Sunspider results: http://ie.microsoft.com/testdrive/benchmarks/sunspider/default.html Hearts: http://www.funonthenet.in/content/view/395/31/ Forrest Gump: http://wallpaper-s.org/36__Forrest_Gump,_1994,_Tom_Hanks,_Robin_Wright_Penn.htm Data: http://walrus.wr.usgs.gov/infobank/programs/html/definition/datadictionary.html