[bug?] arguments.pop() arguments.push() not a function

Dec 24, 2009 at 3:18pm

[bug?] arguments.pop() arguments.push() not a function

When I tried calling pop() and push() on the arguments Array inside an anything() function, Max tells me they are not functions. pop() and push() seem to work as expected in other Arrays. I noticed this problem on Max 5.0.7 and 5.1 on OS X 10.5.8. Here is a bit of JS and a patch demonstrating:

autowatch = 1;

function anything() {
var a = new Array(“a”, “b”, “c”);
a.pop()
for (var i in a)
post(a[i]+”n”);
//post last element in array
post(arguments[arguments.length-1]+”n”);
//pop off last element
arguments.pop();
//print elements in array
for (var i in arguments)
post(arguments[i]+”n”);
}

– Pasted Max Patch, click to expand. –

I’ve thought about this a little more. Is arguments some sort of strange immutable array that allows you to change values, but not size?

#47357
Dec 24, 2009 at 5:58pm

The arguments property is not an array, it just seems a bit like one, its a bit confusing I know. You need to do something like:

var theInput = arrayfromargs(messagename,arguments);

and then you can use array methods like pop() and push() on theInput.

lh

#170437
Dec 27, 2009 at 3:54pm

Interesting, figured it was something like that. Just curious: so what exactly is it. Is this a JavaScript or max feature? Never noticed it in the JavaScript reference.

#170438
Dec 27, 2009 at 4:40pm

It is a feature of javascript itself, have a read here for a bit more information.

lh

#170439
Jan 17, 2010 at 1:14am

First, the code sample is a little wonky – it’s just
var theInput = arrayfromargs(arguments)
(unless there’s something I don’t know about arrayfromargs??)

Second, the arguments array is like a stripped down Array that only has indexing and length – the reason is to get rid of the overheard required in creating a new Array every time you make a function call.

It’s a necessary workaround for Javascript’s lame argument passing (look at Python, much better) but we don’t have to love it…

#170440
Jan 17, 2010 at 2:15am

Ah yes, to avoid confusion I should explain more clearly. The messagename can be used to dynamically process input received using the anything() function. You don’t need to use it if you just want the arguments and you have a specific function already declared.

The first example below will output whatever you send to the input. The second example will output the arguments of a “somename” message but not the string “somename” itself.

lh


function anything() {
    outlet(0,arrayfromargs(messagename,arguments));
}

function somename() {
   outlet(0,arrayfromargs(arguments));
}
#170441

You must be logged in to reply to this topic.