Faced strange issue in JS. I receive error on this:
let a = []
let b = [1,2,3]
b.forEach(a.push)
TypeError: Array.prototype.push called on null or undefined
at Array.forEach (native)
at repl:1:3
at REPLServer.defaultEval (repl.js:262:27)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:431:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:211:10)
at REPLServer.Interface._line (readline.js:550:8)
Sure, I've made a suggestion, that the context is lost. So, I try to accomplish that in this way:
b.forEach([].push.bind(a))
The result becomes unpredictable:
[ 1, 0, [ 1, 2, 3 ], 2, 1, [ 1, 2, 3 ], 3, 2, [ 1, 2, 3 ] ]
WHAT? =) Where is 0 from? Okay, maybe its "glitch"-index, but why not first than? :)
Just to make it clear, this works in a classical way, and that's not a question:
b.forEach(el => a.push(el) )
Can someone explain this strange behaviour?
Because
.forEach()
supplies to your callback 3 arguments.The
.push()
can accept any number of arguments. So, on each iteration,.push()
adds those three arguments to your array.Basically as per the standard syntax of
forEach
, it is having 3 different parameters,current item
,index
and thearray
over which the forEach is called. Sopush
function which was bound witha
will be called every time with those parameters. That is the issue here.Your code will be executed like above.