views:

238

answers:

6

Why does this work (returns "one, two, three"):

var words = ['one', 'two', 'three'];
$("#main").append('<p>' + words.join(", ") + '</p>');

and this work (returns "the list: 111"):

var displayIt = function() {
    return 'the list: ' + arguments[0];
}   
$("#main").append('<p>' + displayIt('111', '222', '333') + '</p>');

but not this (returns blank):

var displayIt = function() {
    return 'the list: ' + arguments.join(",");
}   
$("#main").append('<p>' + displayIt('111', '222', '333') + '</p>');

What do I have to do to my "arguments" variable to be to use .join() on it?

+1  A: 

arguments is not a jQuery object, just a regular JavaScript object. Extend it before you try to call .join(). I think you would write:

return 'the list:' + $(arguments)[0];

(I'm not too familiar with jQuery, only Prototype, so I hope this is not completely bogus.)

Edit: It's wrong! But in his response, Doug Neiner describes what I'm trying to accomplish.

Kevin Conner
This isn't quite right; `arguments` is not an array. Instead, it's an "array-like object".
John Feminella
A normal JS Array supports `join`.
Doug Neiner
if arguments is not an array, how come arguments[0] and arguments[1] give me the correct values passed to the function?
Edward Tanguay
Great, I learned something. Thanks :)
Kevin Conner
Edward: JavaScript objects can respond to [x] like they respond to .foo, it's not something that only array objects can do like in other languages. I think. I have been wrong before ;)
Kevin Conner
@Edward Tanguay: Here's a javascript object that is not an array but looks like one: `{1:'hello',2:'world',length:2}`. Notice that that object only has 3 properties: 1,2 and length. It does not have any methods attached to it. The `arguments` object is similar, the result of getElementsByTagName is another one.
slebetman
+8  A: 

It doesn't work because the arguments object is not an array, although it looks like it. It has no join method:

>>> var d = function() { return '[' + arguments.join(",") + ']'; }
>>> d("a", "b", "c")
TypeError: arguments.join is not a function

To convert arguments to an array, you can do:

var args = Array.prototype.slice.call(arguments);

Now join will work:

>>> var d = function() {
  var args = Array.prototype.slice.call(arguments);
  return '[' + args.join(",") + ']';
}
>>> d("a", "b", "c");
"[a,b,c]"

Alternatively, you can use jQuery's makeArray, which will try to turn "almost-arrays" like arguments into arrays:

var args = $.makeArray(arguments);

Here's what the Mozilla reference (my favorite resource for this sort of thing) has to say about it:

The arguments object is not an array. It is similar to an array, but does not have any array properties except length. For example, it does not have the pop method. ...

The arguments object is available only within a function body. Attempting to access the arguments object outside a function declaration results in an error.

John Feminella
+1...beat me to it.
czarchaic
or `Array.join(arguments, ",")` (like `Array.forEach(arguments, func);`)
Anonymous
both of those work, makes sense, thanks
Edward Tanguay
+1  A: 

Just use the jQuery utility function makeArray

arguments is not an Array, it is an object. But, since it so "array-like", you can call the jQuery utility function makeArray to make it work:

var displayIt = function() {
    return 'the list: ' + $.makeArray(arguments).join(",");
}   
$("#main").append('<p>' + displayIt('111', '222', '333') + '</p>');

Which will output:

<p>the list: 111,222,333</p>
Doug Neiner
A: 

You can use typeof to see what's happening here:

>>> typeof(['one', 'two', 'three'])
"object"
>>> typeof(['one', 'two', 'three'].join)
"function"
>>> typeof(arguments)
"object"
>>> typeof(arguments.join)
"undefined"

Here you can see that typeof returns "object" in both cases but only one of the objects has a join function defined.

Peter McGrattan
+1  A: 

If you are not interested on other Array.prototype methods, and you want simply to use join, you can invoke it directly, without converting it to an array:

var displayIt = function() {
    return 'the list: ' + Array.prototype.join.call(arguments, ',');
};

Also you might find useful to know that the comma is the default separator, if you don't define a separator, by spec the comma will be used.

CMS
A: 

I don't know if there's a simple way to convert arguments into an array, but you can try this:

var toreturn = "the list:";
for(i = 0; i < arguments.length; i++)
{
   if(i != 0) { toreturn += ", "; }
   toreturn += arguments[i];
}
Bernardo Amorim