www.webdeveloper.com
Results 1 to 4 of 4

Thread: arguments and javascript

  1. #1
    Join Date
    Feb 2010
    Posts
    49

    arguments and javascript

    I know there's wonkiness in browsers with the arguments object. Can I do this with all browsers?:

    var e = function(a,b,c){
    return a+b+c;
    }

    var a = function(b,c){
    arguments.push(4);
    return e(arguments);
    }

    a(1,2);
    Last edited by martingaleh; 11-23-2011 at 02:18 PM.

  2. #2
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    What are you on about?

    I am not aware of any "Wonkiness" with the arguments Array, ECMA Script is supported by all browsers with a few quirks in some moz types.

    the arguments array is an array of numbered not associative names, 0,1,2 etc. thats about the only difference.

    Pushing items on to the arguments array I can not vouch for but as its only a local var, its dead when the function returns and finishes, so the action is pointless. If you need to add to the array, you would

    Code:
    function a(b,c){
     return b.concat( c );
     }
    which will concatenate array c with array b and return the new array from the function, so

    z = a( b,c ); // returns array to 'z'

    Does that help?
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  3. #3
    Join Date
    Aug 2007
    Posts
    3,767
    Quote Originally Posted by JunkMale View Post
    Pushing items on to the arguments array I can not vouch for but as its only a local var, its dead when the function returns and finishes, so the action is pointless. If you need to add to the array, you would
    It's adding the elements of the array (well, not technically an array, but near enough), not adding to the array.

    General comment on arguments array: You may encounter a SpiderMonkey (pretty early) bug that didn't allow you to set the arguments[n] element if n was greater than the number of actual arguments, but other that that, I don't think you'll encounter any documented bugs. I'd generally advise avoiding fiddling with the arguments array (again not technically an array) because I don't think it's a good idea, but if you have to, it could work.

    Specific problem: push isn't defined on the arguments object. You can convert it to an array and then push on it, and then pass that, but that's awkward to say the least.
    Code:
    arguments = Array.prototype.slice.call(arguments);
    arguments.push(4); // Defined I think
    I didn't test this, but the other possibility, where order of the parameters as you've defined it isn't vital, would be something like this, which should work (but may not without converting arguments to a real array, sometimes browsers dislike array-like objects where arrays are wanted).
    Code:
    function a(b,c) {
      return e([4].concat(arguments));
    }
    Finally, we may have an alternative and better solution if we know what the precise problem is.
    Great wit and madness are near allied, and fine a line their bounds divide.

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    you don't need slice, extra names or anything, you can just apply all the args at once:
    Code:
    var a = function(){
     return e.apply(this, arguments);
    }
    or, if you want extra (curry), it's simplest to use call:
    Code:
    var a = function(b,c){
     return e.call(this, b, c, 4);
    }
    Last edited by rnd me; 11-23-2011 at 07:44 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles