- These source code examples are on github
1.1 - The arguments.length property is the number of arguments given
One basic feature of the arguments object is the length property, for an example of this feature here is a function that uses the arguments object as a way to return a different result depending on the number of arguments that is given when the function is called. In the event that two arguments are given then the returned result is the sum of those two arguments, however if just one argument is given then the returned result is just that number rather than adding it to undefined.
It might be very basic, but you get the basic idea. The length property of the arguments object will give the number of arguments. There is a bit more to the arguments object though when it comes to the basics, so maybe just a few more basic examples of the arguments object is in order for this section before moving on.
1.2 - The numbered keys of the arguments object contains the values of the arguments
The keys of the arguments are numbered from zero forward just like an array, and the values for each key are the given values for the arguments. The zero index key value pair will be the value for the first argument from the left, and so on from there.
Here is a simple sum function that will add up all the values given to it by way of any and all arguments given to it. The length property of the arguments object is used as a way to find out of one or more arguments where given to the function for starters. In the event that there are one or more arguments then the values are added to a sum value, in the end the sum value is always returned no mater what.
There is more to do with a function such as this such as type checking an so forth, but the basic idea is there. The length property of the arguments object can be used in conjunction with the values of the public keys to do something such as this. However what if I need to know not just the number of arguments that where given, but the set number of parameters that the function has?
1.3 - The function length property will give the parameter count of a function ( the expected number of arguments )
What is the deference between an argument and a parameter? SOme might think that they are just two words the mean the same thing. However there is really a difference between the two. Parameters are often what is refer to as the fixed number of expected values when a function is called, and arguments are the set of values that where actually given.
The function length property will give the expected number of parameters for a function. This value that is a property of the Function prototype can be used in conjunction with the arguments object to know what is expected, and what was actually given.
The arguments object is called the arguments object because it is not an instance of an Array. So it is actually just a plain old object, however it can be considered an array like object because of the way that it is formated. This is because although it is not an instance of an Array it is structured like an array, because its key values are numbered and it has a length property that reflects the number of those numbered key values just like an array. So some Array methods can be used with it via something like Function call method, or a similar Function prototype method.
3 - Simple get function example
The length property can be used to know the number of arguments that where passed to the function when it was called. This can then be used as a way to have more than one expression that is used for something depending on the number of arguments that are given. For example say I want a method that will just get an element in an array by index if it is given one argument, but will use a more complex expression when given two arguments.
There are many other ways that a get function could be written, and in a way in which it can get elements of a grid in more than one way. Later in this post I might get around to a more complex example of this. However the basic use cause example of the arguments object is there never the less. If I give just one argument then it will just get a grid element by index, if I give two then it will get by an x and y position.
4 - Make urls array example of argument object use
I see methods like this all the time, there might be other ways of doing so such as checking if the second argument is an object or a primitive value. However it is nice to know that the arguments object is there as an option when it comes to making a function that behaves such as this.
5 - Weight example
In one of the projects that I am working on I am making all kinds of functions that have to do with figuring how much weight a piece of content has in relation to a given keyword. There are all kinds of ways of how to go about writing such functions that take into account all kinds of factors that might have to do with how relevant a piece of content is in relation to a certain keyword or search term.
In this example I made a weight function that takes a text sample as the first argument and a search term as the second, after that any number of functions that get passed the text and term and figure a weight value that it then returned. The weight function calls each of them that are given and tabulates all of the weight values. naturally I use the arguments object as a way to go about looping over the functions that are given via arguments after the text and search term.
6 - Example making use of an angle, and other optional arguments to set x and y poison of a point
If just one argument is given then that angle is used to find the values for x and y and then that is all. However if a second argument is given this is treated as a distance from the origin, and the additional arguments are used to offset the point object with additional optional offset values.
If I where to comment out the lines of code that have to do with the conditional that checks the arguments object length, then the second use example will result in a point value of zero for both x and y. However because of the check that does not happen, and I am given something more useful. So the arguments object is there to help write functions that will work differently depending on the number of arguments that is given, and it can also be used as an alternative way to get the values of argument apart from the named parameter names when it comes to accessing the key values of this object.
7 - Complex Get Grid Cell example
Now for a more complex version of the get function that I covered in an above example. In the simple get method that method would juts get by a cell index, or a cell position. However what If I am starting to make a far more complex grid module, and now I also want to get a collection of cell objects by a property of a cell object such as type. In that case I just need a slightly more complex version of the same get method, one that will not just check the number of arguments, but also check the type of value that was given. So it can still just return by index of a number is given when used that way, but will also return a collection of cells by type when given a string as the first argument.
8 - Conclusion