Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.call() / .apply() with NO parameters VS simply calling a function with () parenthesis

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.

like image 282
Mark Pieszak - Trilon.io Avatar asked Apr 09 '13 13:04

Mark Pieszak - Trilon.io


1 Answers

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.

like image 119
Arun P Johny Avatar answered Oct 21 '22 00:10

Arun P Johny