Re:Ask HN: Am I the longest-serving programmer – 57 years an...
by kmill on Sunday May 31, 2020 @12:36PM
(#23370653)
Javascript is a lot saner than that. The result is "12". (I'm ok with automatic casting so long as it's an embedding. Every number has a reasonable string representation, but not vice versa. Wanting explicit casts like in Python is defensible, though.)
[ Parent
| on HN ]
Re:Ask HN: Am I the longest-serving programmer – 57 years an...
by SonOfLilit on Sunday May 31, 2020 @12:53PM
(#23370767)
Huh, what do you know.
Still, this is unforgivable in my book:
[] + {} == "[object Object]"
{} + [] == 0
Re:Ask HN: Am I the longest-serving programmer – 57 years an...
by kmill on Sunday May 31, 2020 @01:25PM
(#23370992)
For the first, I frequently (for my own projects) change the Array prototype to have
Array.prototype.toString = function () {
return '[' + this.join(', ') + ']';
};
It would be convenient if Object.prototype.toString by default threw an exception, since the default breaks the embedding rule I described.The second example is misleading, and it has nothing to do with the rules for addition: the {} at the beginning of the expression is parsed as a code block, played off the rules for what the value of a Javascript statement is. More realistically, we have
({}) + [] == "[object Object]"
but the original is, effectively, {
}
console.log(+[]);
Re:Ask HN: Am I the longest-serving programmer – 57 years an...
by SonOfLilit on Sunday May 31, 2020 @02:53PM
(#23371678)
I didn't realise about the second one. Thanks!