javascript - Confused About Resig Curry Example And "arguments" Object -


in this article, john resig discusses snippet currying:

function.prototype.curry = function() {     var fn = this, args = array.prototype.slice.call(arguments);     return function() {       return fn.apply(this, args.concat(         array.prototype.slice.call(arguments)));     };   }; 

i confused expression array.prototype.slice.call(arguments).

  1. here "arguments" "this" argument array's slice() method, "slice()" requires argument itself, think you'd need array.prototype.slice.call(arguments, <someindex>). don't understand how code can working is.

  2. according the docs on "arguments", "arguments" not array, array object. how calling "slice()" on it? , if put in code console.log(arguments.slice()) error saying object not have slice method.

what's going on here?

answer first question:

if call slice on array without arguments, return copy of array. array.prototype.slice.call(arguments) same, operates on arguments object, returning array.

answer second question:

we can call slice on arguments because it's "generic" method (see note here). when pass arguments object this, slice treat array. since arguments array-like object contains length property, works.


Comments