Why do you report an error when you call a method directly using []?

var arr1 = [1, 2]
var arr2 = [3, 4, 5]

// 
[].push.apply(arr1, arr2) // Uncaught SyntaxError: Unexpected token ]

// 
var s = [].push.apply(arr1, arr2)
console.log(arr1)
May.02,2022

is equivalent to

var arr1 = [1, 2]
var arr2 = [3, 4, 5][].push.apply(arr1, arr2)

the reason for the parsing mechanism is caused by not adding a semicolon.


[] only means to create a new array, which is an undeclared variable. If you don't declare it, you don't have to apply to open up storage space. You don't know where to store it and how to get it.

Menu