JavaScript WTFs
-
Upload
pigulla -
Category
Technology
-
view
2.573 -
download
1
Transcript of JavaScript WTFs
![Page 1: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/1.jpg)
JAVASCRIPT WTFS
how I learned to stop worrying
and love the botch that is ECMAScript
![Page 3: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/3.jpg)
WTF?
function foo() {return{
bar: true}
}
// what is x?var x = bar();
![Page 4: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/4.jpg)
Semicolon Insertion
function foo() {return;{
bar: true;};
}
// what is x?var x = bar();
![Page 5: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/5.jpg)
Semicolon Insertion
{ 1 2 } 3
{ 12 } 3
{ 1 2 } 3; ✖
{ 1;2 ;} 3; ✔
i++j
i;++j;
✔
![Page 6: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/6.jpg)
WTF?
var foo = 42;
function bar() {foo = 13;
}
// foo is 42bar();// foo is?
![Page 7: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/7.jpg)
WTF?
var foo = 42;
function bar() {foo = 13;var foo;
}
// foo is 42bar();// foo is?
![Page 8: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/8.jpg)
Declaration Hoisting
var foo = 42;
function bar() {return foo;var foo = 17;
}
// what is the return value of bar() ?bar();
![Page 9: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/9.jpg)
Function Scope
var x = 42;
function foo(n) {x = 13;while (--n > 0) {
var x = 2 * n;}
}
![Page 10: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/10.jpg)
![Page 11: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/11.jpg)
String Theory
// first attemptfunction isString(v) {
return typeof v === ´string´;}
isString(new String(´foo´)); // false!
![Page 12: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/12.jpg)
String Theory
// second attemptfunction isString(v) {
return v instanceof String;}
isString(´oh dear´); // false!
![Page 13: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/13.jpg)
String Theory
// third attemptfunction isString(v) {
return typeof v === ´string´ ||v instanceof String;
}
isString(popup.window.getSomeString());// may return false
![Page 14: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/14.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/15.jpg)
// 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](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/16.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/17.jpg)
The Abstract Equality Comparison Algorithm
![Page 18: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/18.jpg)
What‘s the big deal?
![Page 19: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/19.jpg)
Well, not for everybody.
for (franz=0;franz<items.length;franz++) { var e = items[franz];
...
![Page 20: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/20.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/21.jpg)
![Page 22: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/22.jpg)
![Page 23: JavaScript WTFs](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/23.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/24.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022070321/558c2527d8b42a6b758b45ac/html5/thumbnails/25.jpg)
Source: @markrendle on Twitter