javascript wtfs
TRANSCRIPT
Declaration Hoisting
var foo = 42;
function bar() {
return foo;
var foo = 17;
}
// what is the return value of bar() ?
bar();
String Theory
// first attempt
function isString(v) {
return typeof v === ´string´;
}
isString(new String(´foo´)); // false!
String Theory
// second attempt
function isString(v) {
return v instanceof String;
}
isString(´oh dear´); // false!
String Theory
// third attempt
function isString(v) {
return typeof v === ´string´ ||
v instanceof String;
}
isString(popup.window.getSomeString());
// may return false
String Theory
// fourth attempt
function isString(v) {
return Object.prototype.toString. ⏎
call(v) === ´[object String]´;
}
// until some joker changes
// Object.prototype.toString... :-/
// until someone overwrites String
// or RegExp.test -.-
String Theory
// fifth attempt
function isString(v) {
return typeof v === ´string´ ||
!!v && /^function String\(\) {/⏎
.test(´´ + v.constructor);
}
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
> [] == ![]
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
Why does it matter?
JavaScript is everywhere
you need first class JavaScript developers
weeding out the duds is difficult
jQuery ≠ JavaScript
jQuery
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...