javascript - How does this js snippet work? -


function thunkify(fn) {     var args = [].slice.call( arguments, 1 );     return function(cb) {         args.push( cb );         return fn.apply( null, args );     }; } 

so [] returns array object. slice.call creates new array contents of arguments starting 1 if i'm right.

but how function(cb) work? cb?

function(cb) { ... } creates function.

cb argument passed it.

you when function called.

var thunkified = thunkify(somefunction); thunkified("the value of cb"); 

Comments