What's The Difference Between F(arguments) To F.apply(this,arguments)?
I have not been studying JavaScript for a long time and now I'm trying to implement the Decorator pattern: function wrap(f, before, after){ return function(){ r
Solution 1:
f(arguments)
just callsf
and passes an Array-like object (containing arguments) to it, this is not what you'd want.f.call(this, arguments[0], arguments[1], ..)
would require you to list every argument out and it's pretty much the same asf(arguments[0], arguments[1], ..)
, minus the function context.f.apply(this, arguments)
would callf
and passes each argument inarguments
as actual arguments.
Method #3 is what you'd want if you're trying to implement a wrapper function and not have to consider what arguments are being passed into f
.
Learn more about methods for Function:
call()
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/callapply()
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/applyarguments
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/arguments
Post a Comment for "What's The Difference Between F(arguments) To F.apply(this,arguments)?"