IIFE和电话之间的区别 [英] Difference between IIFE and call

查看:92
本文介绍了IIFE和电话之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之间是否存在差异:

(function(){

}).call(this);

(function(){

})();

var MODULE = {};
(function(){
    this.hello = 'world'
}).call(MODULE);

var MODULE = {};
(function(m){
    m.hello = 'world'
})(MODULE);

我经常在编译的javascript中看到第一个案例。他们都会创建一个范围,并做好他们的命名工作。

I often see the first case in compiled javascript. They both would create a scope and do their namespacing job well.

是否存在任何差异,或仅仅是品味问题。

Is there any difference or is it just a matter of taste.

编辑:为什么编译的javascript会使用IIFE调用?

And why would compiled javascript would use call over IIFE?

推荐答案

(function(){

}).call(this);

调用匿名函数,其中里面调用时,该函数将指向引用的对象。

calls the anonymous function where the this inside the function will point to the object referred by this when the call was made.

(function(){

})();

调用匿名函数,其中里面该函数将指向全局对象(或在严格模式下未定义)

calls the anonymous function where the this inside the function will point to the global object (or undefined in strict mode)

演示:小提琴

这篇关于IIFE和电话之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆