I have a function
function callback(obj){...}
Is it okay to pass in more objects than were declared in the function signature? e.g. call it like this:
callback(theObject, extraParam);
I tried it out on Firefox and it didn’t seem to have a problem, but is it bad to do this?
JavaScript allows this, you can pass any arbitrary number of arguments to a function.
They are accessible in the
argumentsobject which is an array-like object that has numeric properties containing the values of the arguments that were used when the function was invoked, alengthproperty that tells you how many arguments have been used on the invocation also, and acalleeproperty which is a reference to the function itself, for example you could write:The
argumentsobject may look like an array, but it is a plain object, that inherits fromObject.prototype, but if you want to use Array methods on it, you can invoke them directly from theArray.prototype, for example, a common pattern to get a real array is to use the Arrayslicemethod:Also, you can know how many arguments a function expects, using the
lengthproperty of the function object: