0

If I type this and check it out in my Chrome console:

function f(){}
console.dir(f);

What is displayed are these keys:

> arguments
> caller
> length
> name
> prototype
> __proto__

Now, I'm curious if the arguments key on the constructor function is there to aid me in some way visually to see the arguments that are passed to a function, but everytime I pass an argument to a function it fires it off automatically:

function f(a){alert(a)}
console.dir(f("test"));

So, it seems quite useless as an analytic tool. Is this key just here to temporarily hold the arguments and nothing more just for the sake of passing arguments? Or is there something else to this key? I'm sure this is probably a dumb question but I'm curious.

Allan Socks
  • 261
  • 2
  • 10
  • 2
    It allows you to pass in any number of arguments without knowing beforehand how many there are, and iterate them like an array. – Zack Argyle Jul 03 '14 at 04:50
  • 1
    i think it's just an artifact, the arguments _property_ has no use that i've seen... – dandavis Jul 03 '14 at 04:51
  • Thank you. This is exactly what I thought it was, I was just checking. – Allan Socks Jul 03 '14 at 04:52
  • @ZackArgyle: i think you might be thinking of the _arguments_ keyword instead of the external function instance property – dandavis Jul 03 '14 at 04:53
  • Check this http://stackoverflow.com/questions/1007981/how-to-get-function-parameter-names-values-dynamically-from-javascript – c-smile Jul 03 '14 at 05:00
  • A function is also an object, and those are the properties of the Function object, the arguments property is only useful inside the scope of the function (as is caller etc) when it's called with unknown arguments. – adeneo Jul 03 '14 at 05:05
  • 2
    Have a look at the MDN documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/arguments – Felix Kling Jul 03 '14 at 05:12

3 Answers3

1
  1. The arguments object is a local variable available within all functions.

  2. You can refer to a function's arguments within the function by using the arguments object. This object contains an entry for each argument passed to the function.

  3. if a function is passed three arguments, you can refer to the argument as follows:

    arguments[0] arguments[1] arguments[2]

Reference link arguments.

Srinu Chinka
  • 1,471
  • 13
  • 19
  • He's asking about the `.arguments` property on the function object. – cookie monster Jul 03 '14 at 05:16
  • 1
    In JavaScript every function is actually a Function object. **arguments** is a _property_ of **Function** and arguments is deprecated as property of Function, but we can use the **arguments** object available within the our function to represent the passed arguments to the our function. – Srinu Chinka Jul 03 '14 at 05:25
  • I know all that. Your answer made no reference to the function property that he's specifically asking about. Your comment is more relevant to the question. – cookie monster Jul 03 '14 at 14:26
  • @cookiemonster thank you :), next time i won't make this kind mistake :) – Srinu Chinka Jul 04 '14 at 04:40
0

arguments is an array like object that is available in function objects in javascript. It allows a function a way to account for arguments that were used while invoking a function but do not have a parameter specifically assigned to it.

var mul = function ( ) {
   var i, total = 0;
   for (i = 0; i < arguments.length; i += 1) {
      total *= arguments[i];
   }
   return total;
};
document.writeln(mul(4, 8, 2)); //64

Source: http://goo.gl/hKpFGl This explanation is pretty much ripped directly from here. If you want to get familiar with some good patterns javascript offers this isn't a bad place to start.

Teeknow
  • 1,015
  • 2
  • 17
  • 39
0

arguments is an Array-like object corresponding to the parameters passed to a function.

For Example:

function callMe (a, b, c){
 console.log(arguments);
}

callMe(); // return empty array []
callMe(1,2); // return array [1, 2]
callMe(1,2,3); // return empty array [1, 2, 3]
callMe(1,2,3,4); // return empty array [1, 2, 3, 4]

For more help, read this doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments

Mohit Pandey
  • 3,679
  • 7
  • 26
  • 38