I'm writing several functions that take different types of parameters. As such, it's much simpler to just do:
var myFunc = function() {
var args = Array.prototype.slice.call(arguments)
}
...than to try to handle all the different possibilities directly in the parameter field of the function declaration, e.g.:
var myFunc = function(param1, param2, param3) {
if (param3) {
// etc.
}
}
Array.prototype.slice.call(arguments)
adds a lot to the code though, and reduces readability. I'm trying to figure out if there's a way to write a helper function that could accomplish the same thing as Array.prototype.slice.call()
, but cleaner and easier to read. I was trying something like:
var parseArgs = function() {
return Array.prototype.slice.call(arguments)
}
var foo = function() {
console.log(parseArgs(arguments))
}
foo('one', 'two', 'three')
// [Arguments, ['one', 'two', 'three']]
But obviously that doesn't work.