Skip to content Skip to sidebar Skip to footer

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:

  1. f(arguments) just calls f and passes an Array-like object (containing arguments) to it, this is not what you'd want.

  2. f.call(this, arguments[0], arguments[1], ..) would require you to list every argument out and it's pretty much the same as f(arguments[0], arguments[1], ..), minus the function context.

  3. f.apply(this, arguments) would call f and passes each argument in arguments 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:

Post a Comment for "What's The Difference Between F(arguments) To F.apply(this,arguments)?"