【问题标题】:Usage of parameters for IIFE and namespaceIIFE 和命名空间参数的使用
【发布时间】:2019-07-09 15:28:47
【问题描述】:

我正在浏览一些使用 google maps API 的代码。

function initMap(){
(function(google , window){
var center = {
    center: {lat: -34.397, lng: 150.644},
    zoom: 8
};
var map = new google.maps.Map(document.getElementById('map'), center);    
})(google , window);
}

所以我了解 IIFE 以及它如何防止全局范围污染,但我不明白在上面的代码中将全局对象 google 和 window 传递给 IIFE 是什么。 有人能解释一下使用这种模式有什么好处吗?我们究竟在 IIFE 中使用 google 和 window 对象做什么?

【问题讨论】:

  • 其实挺没意思的。

标签: javascript


【解决方案1】:

我们对 IIFE 中的 google 和 window 对象究竟做了什么?

没什么用,也没什么用。

window 在函数内部根本没有使用。

google 被使用,但是是同步的。如果它是异步使用的,那么传递它可能会很有用,因此它的值不会得到overwritten between multiple uses of it,但这里不是这种情况。

【讨论】:

    猜你喜欢
    • 2015-12-21
    • 2016-07-16
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 2013-11-13
    • 2021-07-15
    • 1970-01-01
    相关资源
    最近更新 更多