![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
So is the first argument to slice really optional despite the various doc's I've checked (including and besides the book in question), or are Firefox and Safari both working outside of the JS spec here, or is there something else I'm missing? |
#3
| |||
| |||
|
|
I bought Crockford's "javascript: The Good Parts" yesterday to help build my JavaScript foo. On page 44, he gives an implementation of the curry function: Function.method('curry', function() { var slice = Array.prototype.slice, args = slice.apply(arguments), that = this; return function() { return that.apply(null, args.concat(slice.apply(arguments))); }; }); I'm trying to reconcile the two invocations of slice.apply(arguments) with the notion that the slice method in Array's prototype has two arguments, the second of which is optional. I thought that this would mean that those invocations would need to be slice.apply(arguments,0) in order to supply the start argument, and in "leap before you look" mode I submitted an erratum to O'Reilly. Now however, I've actually tried the code in both Firefox and Safari, and it seems to work as it's given in the book. So is the first argument to slice really optional despite the various doc's I've checked (including and besides the book in question), or are Firefox and Safari both working outside of the JS spec here, or is there something else I'm missing? |
#4
| |||
| |||
|
|
RubyRedRick wrote: I bought Crockford's "javascript: The Good Parts" yesterday to help build my JavaScript foo. On page 44, he gives an implementation of the curry function: Function.method('curry', function() { var slice = Array.prototype.slice, args = slice.apply(arguments), that = this; *return function() { * *return that.apply(null, args.concat(slice.apply(arguments))); }; }); A much more interesting question is why use - apply - when you could use - call -? Not that the former is a mistake (the handling of the undefined second argument is fully specified) but the - apply - method has a length of 2 and the call method has a length of 1, which suggests it should be the one to use when there is only going to be one argument. |
#5
| |||
| |||
|
|
"Richard Cornford" wrote: RubyRedRick wrote: I bought Crockford's "javascript: The Good Parts" yesterday to help build my JavaScript foo. On page 44, he gives an implementation of the curry function: Function.method('curry', function() { var slice = Array.prototype.slice, args = slice.apply(arguments), [1]--------------^^^^^ |
|
that = this; return function() { return that.apply(null, args.concat(slice.apply(arguments))); [2]----------------------------------------------^^^^^ |
|
}; }); A much more interesting question is why use - apply - when you could use - call -? Not that the former is a mistake (the handling of the undefined second argument is fully specified) but the - apply - method has a length of 2 and the call method has a length of 1, which suggests it should be the one to use when there is only going to be one argument. I don't see how call could be used here. The method is being used as a trick to obtain a real array object from arguments which is a pseudo- array and lacks methods. |
![]() |
| Thread Tools | |
| Display Modes | |
| |