Weird (but useful) JavaScript

... but isn't all JavaScript weird?

Andrew Chilton <chilts@appsattic.com>
http://www.appsattic.com/

Comparative Size of Books

Comparative Size of Books

Comparative Size of Books

Comparative Size of Books

Comparative Size of Books

Comparative Size of Books

Type Coercion - The Wrong Way

''        == '0'        // false
0         == ''         // true
0         == '0'        // true

false     == 'false'    // false
false     == '0'        // true

false     == undefined  // false
false     == null       // false
null      == undefined  // true

' \t\r\n' == 0          // true

Type Coercion - The Right Way

''        === '0'       // false
0         === ''        // false
0         === '0'       // false

false     === 'false'   // false
false     === '0'       // false

false     === undefined // false
false     === null      // false
null      === undefined // false

' \t\r\n' === 0         // false

Functions as Methods

var lion = { noise : 'roar' };
var bear = { noise : 'growl' };
var cat  = { noise : 'meow' };

var speak = function(say) {
    console.log(say + this.noise + '!');
}

speak.call(lion, 'Hear me '); // Hear me roar!
speak.call(bear, 'Make me '); // Make me growl!
speak.call(cat,  'I say '  ); // I say meow!

speak.apply(lion, [ 'Hear me ' ]); // Hear me roar!
speak.apply(bear, [ 'Make me ' ]); // Make me growl!
speak.apply(cat,  [ 'I say '   ]); // I say meow!

Passing an Index to a Handler (jQuery)

var items = [ 'red', 'green', 'blue' ];

for ( var i = 0; i < items.length; i++ ) {

    $('#' + items[i]).click(function(ev) {
        console.log('This is item number ' + i);
    });

}

Passing an Index to a Handler (jQuery)

var items = [ 'red', 'green', 'blue' ];
for ( var i = 0; i < items.length; i++ ) {
    $('#' + items[i]).click((function(shadow) {
        return function(ev) {
            console.log('This is item number ' + shadow);
        };
    })(i));
}
var items = [ 'red', 'green', 'blue' ];
items.forEach(function(v, i) {
    $('#' + v).click(function(ev) {
        console.log('This is item number ' + i);
    });
});

Object Methods as Event Handlers

var obj = {
    myHandler : function() { ... },
};

var handler = obj.myHandler();

// .. but 'this' is the global object
$('a').click(handler);
var boundHandler = handler.bind(obj);

// 'this' is now obj and can't be changed
$('a').click(boundHandler);

sum()

function sum() {
    var i,
        n = arguments.length,
        total = 0;
    for ( i = 0; i < n; i++ ) {
        total += arguments[i];
    }
    return total;
}
function sum() {
    return arguments.reduce(function(a, b) {
        return a + b;
    }, 0);
}

With Statement - The Wrong Way

with (obj) {
    foo = bar; // what does this mean?
}

obj.foo = bar;
obj.foo = obj.bar;
foo = bar;
foo = obj.bar;

With Statement - The Right Way

with (obj) {
    foo = bar; // what does this mean?
}

obj.foo = bar;
obj.foo = obj.bar;
foo = bar;
foo = obj.bar;

Where JavaScript got it Right!

Thanks!