April 15, 201016 yr Could anyone help this nooby understand function "arguments", what are arguments exactly?? and info on passing them etc thanks people
April 15, 201016 yr In any function, the arguments are the data that you send to the function when you call it. If, for example, you function looked like this: function epicFunction(foo, bar, glee) {... your arguments would be "foo", "bar" and "glee". When the function is called, data is passed to it in the brackets. To call the above function and giv it some data, you'd use a pice of code like this: epicFunction('fubab', 12, ['singing', 'dancing']); Now, inside our epicFunction, "foo" refers to a string containing "fubab", "bar" is the number 12 and "glee" is an array with singing and dancing. On a slightly more advanced level, each function has an object associated with it called "arguments" which allows you to reference any and all arguments much like you would an array. In the above example, "foo" would be the equivalent of "arguments[0]", "bar" would be "arguments[1]" and so on. "arguments.length" will tell you how many arguments have been passed to the function. This is how things like the $.extend() method in jQuery allow you to pass any number of objects to it. The arguments object has another remarkable handly property "callee", which refers to the function itself. I've seen a few coders use "arguments.callee" to create an external reference to the state of the function. If you consider the code: function epicFunction(foo, bar, glee) { if (foo == "fubab") { arguments.callee.broken = true; } else { arguments.callee.broken = false; } } then in any other function, you can check to see whether the last value of "foo" passed to "epicFunction" was "fubab" like this: function otherFunction() { if (epicFunction.broken) { alert("D'oh!"); } else { alert("Woo Hoo!"); } } There is another property called "caller" which will return the name of the function, but it's been depreciated of late and I've never thought of a good reason for it anyway. I think that's about all there is to know.
April 15, 201016 yr Author wooow thank you soo much, that was an amazing answer, again, thank you so much you couldnt have been more clear and helpful, thanks!
Create an account or sign in to comment