JavaScript WTFs

25
JAVASCRIPT WTFS how I learned to stop worrying and love the botch that is ECMAScript

Transcript of JavaScript WTFs

Page 1: JavaScript WTFs

JAVASCRIPT WTFS

how I learned to stop worrying

and love the botch that is ECMAScript

Page 2: JavaScript WTFs

About me

Raphael PigullaBörseGo AGLead Developer JavaScript

[email protected]@muddyb0y

Page 3: JavaScript WTFs

WTF?

function foo() {return{

bar: true}

}

// what is x?var x = bar();

Page 4: JavaScript WTFs

Semicolon Insertion

function foo() {return;{

bar: true;};

}

// what is x?var x = bar();

Page 5: JavaScript WTFs

Semicolon Insertion

{ 1 2 } 3

{ 12 } 3

{ 1 2 } 3; ✖

{ 1;2 ;} 3; ✔

i++j

i;++j;

Page 6: JavaScript WTFs

WTF?

var foo = 42;

function bar() {foo = 13;

}

// foo is 42bar();// foo is?

Page 7: JavaScript WTFs

WTF?

var foo = 42;

function bar() {foo = 13;var foo;

}

// foo is 42bar();// foo is?

Page 8: JavaScript WTFs

Declaration Hoisting

var foo = 42;

function bar() {return foo;var foo = 17;

}

// what is the return value of bar() ?bar();

Page 9: JavaScript WTFs

Function Scope

var x = 42;

function foo(n) {x = 13;while (--n > 0) {

var x = 2 * n;}

}

Page 10: JavaScript WTFs
Page 11: JavaScript WTFs

String Theory

// first attemptfunction isString(v) {

return typeof v === ´string´;}

isString(new String(´foo´)); // false!

Page 12: JavaScript WTFs

String Theory

// second attemptfunction isString(v) {

return v instanceof String;}

isString(´oh dear´); // false!

Page 13: JavaScript WTFs

String Theory

// third attemptfunction isString(v) {

return typeof v === ´string´ ||v instanceof String;

}

isString(popup.window.getSomeString());// may return false

Page 14: JavaScript WTFs

String Theory

// fourth attemptfunction isString(v) {

return Object.prototype.toString. ⏎

call(v) === ´[object String]´;}

// until some joker changes// Object.prototype.toString... :-/

Page 15: JavaScript WTFs

// until someone overwrites String// or RegExp.test -.-

String Theory

// fifth attemptfunction isString(v) {

return typeof v === ´string´ ||!!v && /^function String\(\)

{/⏎.test(´´ + v.constructor);

}

Page 16: JavaScript WTFs

WTF?

(1) <Array>[] == !<Array>[](2) <Array>[] == !ToBoolean(<Array>[])(3) <Array>[] == <Bool>false(4) <Array>[] == ToNumber(<Bool>false)(5) <Array>[] == <Number>0(6) ToPrimitive(<Array>[]) == <Number>0(7) <String>[].toString() == <Number>0(8) <String>´´ == <Number>0

> [] == ![]

Page 17: JavaScript WTFs

The Abstract Equality Comparison Algorithm

Page 18: JavaScript WTFs

What‘s the big deal?

Page 19: JavaScript WTFs

Well, not for everybody.

for (franz=0;franz<items.length;franz++) { var e = items[franz];

...

Page 20: JavaScript WTFs

It turns out that if you have absolutely no idea what you‘re doing in the language, you can still generally make things work.

“„

Douglas Crockford

Page 21: JavaScript WTFs
Page 22: JavaScript WTFs
Page 23: JavaScript WTFs

Why does it matter?

JavaScript is everywhere you need first class JavaScript developers weeding out the duds is difficult jQuery ≠ JavaScript

jQuery

Page 24: JavaScript WTFs

The Checklist™

closure vs. anonymous function

functional vs. imperative languages

prototypical vs. class-based inheritance

asynchronous vs. multi-threaded code

What is the difference between...

Page 25: JavaScript WTFs

Source: @markrendle on Twitter