简体   繁体   English

javascript函数后括号中的参数

[英]Parameters in bracket after a javascript function

I found a solution for google maps marker on stack-overflow. 我在堆栈溢出时找到了谷歌地图标记的解决方案。 Here is the link. 链接在这里。 Google Maps API Multiple Markers with Infowindows 带有Infowindows的Google Maps API多个标记

google.maps.event.addListener(marker,'click', (function(marker,content,infowindow){ 
return function() {
    infowindow.setContent(content);
    infowindow.open(map,marker);
};
})(marker,content,infowindow)); 

My question is: What is the purpose of (marker,content,infowindow) being placed behind the javascript function? 我的问题是:(标记,内容,信息窗口)放在javascript函数后面的目的是什么? And what is it called? 它叫什么? Thank you very much in advanced. 非常感谢您的进步。

这是一个自调用函数 - 这意味着在声明之后立即使用这些参数调用它

It's a IIFE (Immediately Invoked Function Expression), the second parenthesis invokes the function and allows you to pass arguments to the function 它是一个IIFE(立即调用函数表达式),第二个括号调用函数并允许您将参数传递给函数

(function () {
    //statements
})();

Check docs here 在这里查看文档

Please see the answer by Damian for the official name. 请查看Damian的正式名称答案。

However, this is simply executing the function created by executing the first function. 但是,这只是执行通过执行第一个函数创建的函数。

If we breakup the code into multiple lines, it may make more sense: 如果我们将代码分解成多行,可能会更有意义:

const makeAFuction = function(marker,content,infowindow){ 
  return function() {
    infowindow.setContent(content);
    infowindow.open(map,marker);
  };
}
const listener = makeAFunction(marker,content,infowindow);
google.maps.event.addListener(marker,'click', listener); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM