.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.
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.
上一篇: 什么是console.log?