SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Non-Member DelvarWorld's Avatar
    Join Date
    Jul 2004
    Location
    Baloney
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    arguments is not an array?

    This seems a little counter intuitive, but I thought that arguments was an array?
    Code JavaScript:
    function hi() {
        alert(arguments);
        alert(arguments.join(',');
    }
    hi('cheese', 'cow');
    It says join is not a function. You can loop through the arguments object with a for loop (not for in), but it doesn't have any array prototype methods like join(). Does anyone have any insight as to why it's represented as an object and not a function? And the best thing to do about it to make it array accessible? I did find this awesome solution:
    Code JavaScript:
    var args = [].splice.call(arguments,0);
    which makes the array "args" from arguments. Might be better in an object.prototype. Anyone else encounter / deal with this?

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. However it can be converted to an real array:
    Code JavaScript:
     var args = Array.prototype.slice.call(arguments);
    SOURCE: https://developer.mozilla.org/En/Cor...cope/arguments
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    That slice method requires a begin argument, so the code should instead be

    Code javascript:
    var args = Array.prototype.slice.call(arguments, 0);

    However, there are other ways to approach this too.

    You can also use the Array object to directly perform the needed task.

    Code javascript:
    alert(Array.prototype.join.call(arguments, ', '));

    And you can also create a toArray function so that anything can be converted to an array with toArray(arguments)

    Code javascript:
    function toArray(obj) {
        return Array.prototype.slice.call(obj, 0);
    }
    ...
    alert(toArray(arguments).join(', '));

    Or if you want to get fancy, you can provide a default toArray method for all objects so tha you can use arguments.toArray()

    Code javascript:
    Object.prototype.toArray = function () {
        return Array.prototype.slice.call(this, 0);
    };
    ...
    alert(arguments.toArray().join(', '));
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •