arrays - No semicolon before [] is causing error in Javascript? -


var = [1,2,3,4]; var b = [10,20,30,40]; console.log([a,b].length) [a,b].some(function(x){ x.push(x.shift()) }); 

i extremely surprised today when code caused

[a,b].some(function(x){ x.push(x.shift()) });       ^ typeerror: cannot call method 'some' of undefined 

obviously javascript 'auto semicolon insertion' not working expected here. why?

i know might recommend use ; everywhere avoid that, question not whether better use ; or not. love know happens here?

when i'm worried semicolon insertion, think lines in question without whitespace between them. in case, be:

console.log([a,b].length)[a,b].some(function(x){ etc }); 

here you're telling javascript engine call console.log length of [a,b], @ index [a,b] of result of call.

console.log returns string, code attempt find property b of string, undefined, , call undefined.some() fails.

it's interesting note str[a,b] resolve str[b] assuming str string. kamil points out, a,b valid javascript expression, , result of expression b.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -