At times, you will need to use the arguments object of a function as an array. In order to convert the arguments into an array, you can do the following:


// Takes an arguments object and returns it as an array.
function toArray(objArgs) {
  return [].slice.call(objArgs, 0);
}

The following is an example of using this function:


function cdr() {
  return toArray(arguments).slice(1);
}

alert(cdr(1,2,3,4));  // 2,3,4

If you are not familiar with CDR, it is an operator available in LISP which basically returns all elements except the first one.

Categories: BlogJavaScriptJScript

Leave a Reply

Your email address will not be published. Required fields are marked *