Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.pop in Node skips and forgets a function item

I had an idea to store code as lists (arrays) in Node and execute them, but this is harder than I thought: if I make a list with a function in the beginning or in the end, .pop or .shift removes but omits it and returns the next element:

> l = [1, 75, 84, function() { console.log('aseuht') }]
[ 1, 75, 84, [Function] ]
> l.pop()
84
> l
[ 1, 75 ]

I noticed this in Node v0.4.9, but it is still present in 0.6.10.

Is there a workaround for that?

update: I filed an issue on GitHub.

update 2: the bug is present only in my shell, when I run these commands from a standalone script, it works correctly. So this is just a shell issue.

like image 525
culebrón Avatar asked Oct 09 '22 15:10

culebrón


1 Answers

I just tried it in Node, Firefox, and Chrome and it worked as expected in all three.

here's the code I used:

var l= [1, 75, 84, function() { console.log('aseuht'); }];
console.log(l);
console.log(l.pop());
console.log(l);

and result:

[ 1, 75, 84, [Function] ]
[Function]
[ 1, 75, 84 ]

and the JSFiddle: http://jsfiddle.net/Q5M8M/

What version of Node are you using?

like image 199
Jonathan Rowny Avatar answered Oct 12 '22 12:10

Jonathan Rowny