3 ms·
For the first, I frequently (for my own projects) change the Array prototype to have Array.prototype.toString = function () { return '[' + this.join('
by kmill 6y ago
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(+[]);
- SonOfLilit 6y agoI didn't realise about the second one. Thanks!
- skykooler 6y agoThanks, I never realized that the second one was parsed as a code block rather than an object!