Eventuali duplicati:
What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk for CodeMash 2012?Perché {} + [] restituisce 0 in Javascript?
So che quando []
è costretto ad una stringa restituisce la stringa vuota (""
), e quando {}
è costretto ad una stringa restituisce "[object Object]"
.
Quando eseguo [] + {}
nella console JavaScript del mio browser, restituisce come mi sarei aspettato:
>> [] + {}
"[object Object]"
Ma quando ho eseguito {} + []
, restituisce un valore del tutto inaspettato:
>> {} + []
0
Cosa poteva far sì che restituisca 0
?
Sembra che si aggiunga null a null. Questo sarebbe equivalente a 0 + 0. Questa è solo un'ipotesi. – Trisped
@Trisped: nessuno di questi è nulla. – SLaks
Questa domanda è riassunta da http://stackoverflow.com/questions/9032856/what-is-the-explanation-for-these-bizarre-javascript-behaviours-mentioned-in-the (che ad oggi ha 301 voti) ..... –