I’m trying to push multiple elements as one array, but getting an error:
> a = []
[]
> a.push.apply(null, [1,2])
TypeError: Array.prototype.push called on null or undefined
I’m trying to do similar stuff that I’d do in ruby, I was thinking that apply is something like *.
>> a = []
=> []
>> a.push(*[1,2])
=> [1, 2]
When using most functions of objects with
applyorcall, thecontextparameter MUST be the object you are working on.In this case, you need
a.push.apply(a, [1,2])(or more correctlyArray.prototype.push.apply(a, [1,2]))