2

I found a solution for google maps marker on stack-overflow. Here is the link. Google Maps API Multiple Markers with Infowindows

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? And what is it called? Thank you very much in advanced.

Stan
  • 131
  • 1
  • 8

3 Answers3

1

That is a self invoking function - that means it is immediately called with these parameters, just after declaration

Daut
  • 2,537
  • 1
  • 19
  • 32
1

It's a IIFE (Immediately Invoked Function Expression), the second parenthesis invokes the function and allows you to pass arguments to the function

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

Check docs here

Damian Peralta
  • 1,846
  • 7
  • 11
1

Please see the answer by Damian for the official name.

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); 
Vlad274
  • 6,514
  • 2
  • 32
  • 44