.call()/ .apply()与NO参数VS简单地用()括号调用一个函数 [英] .call() / .apply() with NO parameters VS simply calling a function with () parenthesis
问题描述
我已经看到它在代码中的表现完全不同,但是对于常规的() .call / .apply / strong>函数执行。
这当然是一个过分简化的例子
var func = function(){/ *做任何* /};
func.call();
func.apply();
VERSUS 就是简单的圆括号。
func();
在这个地方没有看到任何信息,我知道为什么call / apply在params当你使用 func();
调用一个方法时, ,此方法中的变量指向
window
object。
当您使用拨打(...) / apply(...)传递给方法调用的第一个参数在该方法内变为 this
。如果你没有传递任何参数/传递null或undefined,那么这个
会以非严格模式成为全局对象。
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.
这篇关于.call()/ .apply()与NO参数VS简单地用()括号调用一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!