JavaScript 自执行函数 - 有什么区别?
我通过使用 jQuery 非常熟悉自执行函数。
(function($) { /* do stuff */ })(jQuery);
今天我正在阅读backbone.js 源代码,注意到他们这样做:
(function() { /* do stuff */ }).call(this);
这实现了同样的事情吗?下面的两行代码会做同样的事情吗?
(function($) { /* do stuff */ })(jQuery);
(function($) { /* do stuff */ }).call(jQuery);
I am very familiar with self executing functions from working with jQuery.
(function($) { /* do stuff */ })(jQuery);
Today I was reading the backbone.js source and noticed that they do this:
(function() { /* do stuff */ }).call(this);
Is this achieving the same thing? Would the following 2 lines of code do the same thing?
(function($) { /* do stuff */ })(jQuery);
(function($) { /* do stuff */ }).call(jQuery);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一种形式是传入参数,而第二种形式是在执行函数中设置“this”所指的内容。他们是不同的。
有关更多信息,请参阅
Function.prototype.call< /code>
和
Function.prototype.apply
。这是您可能想要使用
call
技术的情况:通过
call()
获取this
的值,自调用函数在 Global(窗口)范围内调用,而不是当前对象。The first form is passing in a parameter, while the second form is setting what 'this' refers to inside the executing function. They are different.
For more information, see
Function.prototype.call
andFunction.prototype.apply
.Here's a case where you might want to use the
call
technique:Without setting the value of
this
viacall()
, the self-calling function is invoked in the scope of the Global (window), not the current object.在他们的例子中:
...他们确保使用
this
的特定值调用该函数(例如,函数中的this
与this
相同) code>this 在函数之外)。在您的情况下:
...您的函数中的
this
将是全局对象,因为您调用它时没有执行任何设置this
的操作,因此this
默认为全局对象(window
,在浏览器上)。在 JavaScript 中,
this
完全由函数的调用方式设置。有两种主要方法:通过调用函数作为从对象属性检索函数的表达式的一部分,例如
在这种情况下,在对
foo
的调用中,this
将引用obj
。通过内置的
call
调用该函数 或应用
方法:call
和apply
之间的唯一区别在于如何指定要传递到正在调用的函数中的参数:使用call
,您可以只需将它们作为离散参数列出在您想要的this
值后面即可;使用apply
,您可以将它们作为数组提供。In their case:
...they're making sure that the function gets called with a specific value for
this
(e.g.,this
within the function will be the same asthis
outside of the function).In your case:
...
this
within your function will be the global object, because you've called it without doing anything that setsthis
, and sothis
defaults to the global object (window
, on browers).In JavaScript,
this
is set entirely by how a function is called. There are two chief ways:By calling the function as part of the expression that retrieves it from an object property, e.g.
In that case, within the call to
foo
,this
will refer toobj
.By calling the function via its built-in
call
orapply
methods:The only difference between
call
andapply
is how you specify arguments to pass into the function you're calling: Withcall
, you just list them as discrete arguments following thethis
value you want; withapply
, you supply them as an array.第一个将 jQuery 作为参数 $ 传递。第二个使 jQuery 成为函数内的“this”变量。
The first passes jQuery as the parameter $. The second makes jQuery the 'this' variable inside the function.
.call
将函数置于闭包作用域中。因此,如果您这样做:
$
将是undefined
.call
puts the function in a closure scope.So if you did this:
$
would beundefined