Why I Hate JavaScript
Mafinar Rashid Khan
Product Manager
Panacea Systems Ltd
@mafinar
Why should anybody care?
Alternative Title...
- ["W", "T"] + "FJS"
-
ঝালটক JS - JSOddities;
Let's begin...
My JS Belt
- JavaScript (the Language)
- AngularJS
- ReactJS (+ NuclearJS Redux MobX)
- KnockoutJS
- jQuery
Framework != Language
or `Framework !== Language`
A bit of history...
- Initial design in 10 days
- Wanted to be Scheme, ended up C-ish
- Prototype based language, with Java like syntax
- ... and now fake classes/inheritance
Some more...
- Too many false friends
- null, undefined, NaN
- null is an object (?)
- Type coersion hell
- Weakly typed
- And then this...
Let's show some codes...
[] == ![]
typeof(null) == 'object'
typeof(NaN) === 'number'
NaN !== NaN
Number({}) != Number([])
Fun with None...
I really want to see that BNF
1.toFixed(2) //Say what?
1 . toFixed(2) // '1.00'
1 .toFixed(2) // '1.00'
Enemy of Common Sense
[,,] //= [undefined, undefined, undefined]
undefined + "HELLO" === 'undefinedHELLO'
[,,].join() //= ','
// Even in JS logic, should it not be
// 'undefined,undefined' ???
// By the way, this is totally valid...
undefined = "Mafinar Khan"
// :D it's not a keyword.
Y u so forgiving???
var nums = [1, 2, 3]
nums.splice(nums.indexOf('wtf'), 1)
nums; // [1, 2]
parseInt('F***', 16) // 15
typeof + "WTF" // Number
"10" + 10 //== "1010"
"10" * 10 //== 100
There's more...
- Too many scoping rules
- Unintuitive "this"
- Convoluted truthies and falsies
- Inconsistent library behaviors
- OMG Coercions!
o1 = { hello: “world” };
o2 = Object.create(null);
o2.hello = “world”;
o1 + “”; // “[object Object]”
o2 + “”; // TypeError!
s = Symbol(“that’s cool”);
s; // Symbol(that’s cool)
String(s); // "Symbol(that’s cool)”
s + “”; // TypeError!
function foo() {
try {
return 2;
}
finally {
return 3;
}
}
But ES6 has classes...
Have you heard of Halal Champagne?
Classes are unnatural in Prototype based Language and only confuses you further
But all these oddities don't come in real life...
What Helps Me
Why I Hate JavaScript
By Mafinar Khan
Why I Hate JavaScript
- 4,984