8

I've seen it done differently in code out there, but is there any benefit or reason to doing a (blank params) .call / .apply over a regular () function execution.

This of course is an over-simplified example

var func = function () { /* do whatever */ };

func.call();
func.apply();

VERSUS just the simple parenthesis.

func();

Haven't seen any information on this anywhere, I know why call/apply are used when params are passed.

7
  • 3
    @FeistyMango: Definitely not a dupe of that. Commented Apr 9, 2013 at 13:54
  • Where have you seen such code? I thought (and answered) that it was to make sure this is empty, but that's not the case. As far as I can tell through some quick experiments, it's a pointless practice. Commented Apr 9, 2013 at 13:59
  • Sure it is. He is asking the benefit/reasons for call vs apply. Isn't that essentially the same as asking the difference between the two??? Commented Apr 9, 2013 at 13:59
  • Nope, he's asking the reasons to use .call or .apply without a this argument versus simply calling the function. Commented Apr 9, 2013 at 14:00
  • @FeistyMango I was putting the 2 in the same category, I was looking more at why would someone do a blank param funcName.call(); versus funcName(); Commented Apr 9, 2013 at 14:00

2 Answers 2

6

When you call a method with func();, this variable inside the method points to window object.

Where as when you use call(...)/apply(...) the first parameter passed to the method call becomes this inside the method. If you are not passing any arguments/pass null or undefined then this will become global object in non strict mode.

Sign up to request clarification or add additional context in comments.

5 Comments

Nope, this will still point to the global object, unless you explicitly pass undefined as the first argument.
Bart is correct - in fact this will be window even when you explicitly pass null or undefined. In "strict" mode it works, but then just plain func() also gets this set to undefined.
Wow very interesting, definitely good to know! So maybe some of the code I had seen (wish I could find an example right now) was in Strict mode intending it to be undefined, not sure why they'd want that..
Then again, if you're in strict mode, this will not reference the global object either if a simple func() call is made. So even in strict mode, I don't see any reason to use func.call() without a this argument.
0

Yes, there is an important difference in some cases. For example, dealing with callbacks. Let's assume you have a class with constructor that accepts callback parameter and stores it under this.callback. Later, when this callback is invoked via this.callback(), suddenly the foreign code gets the reference to your object via this. It is not always desirable, and it is better/safer to use this.callback.call() in such case.

That way, the foreign code will get undefined as this and won't try to modify your object by accident. And properly written callback won't be affected by this usage of call() anyways, since they would supply the callback defined as an arrow function or as bound function (via .bind()). In both such cases, the callback will have its own, proper this, unaffected by call(), since arrow and bound functions just ignore the value, set by apply()/call().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.