0

JSHint is throwing following warning:

Functions declared within loops referencing an outer scoped variable may lead to confusing semantics. This is about creating a google maps project. I didn't find this exact problem in stackoverflow.

Any idea how to fix this it?

// The error refers on this line:
marker.addListener('click', function() {

  // here is all the function: 
  marker.addListener('click', function() {
    populateInfoWindow(this, largeInfowindow);
  });
}

my gitHub Repo: https://github.com/Heleni/Google_Maps

Mines
  • 3
  • 5
  • please add all of the code and make sure it is indented 4 times so that it is visible – oldboy Aug 09 '17 at 07:21
  • I guess it is pointing to the fact that you are using `marker.addListener(` inside a function and `marker` is defined outside. Try using `this` instead. Also `.addEventListener` inside another `addEventListener` is a bad idea – Rajesh Aug 09 '17 at 07:21
  • where's the loop – passion Aug 09 '17 at 07:22
  • hi guys, thanks for your help. i just post my gitHub Repo if you would like to see more clear where is the problem. I did declare the marker before this function because i need to use it again before the function – Mines Aug 09 '17 at 07:35
  • i use" this" as Rajesh said but didnt work, i think i did use it wrong, could you change my function the way you think it will work? – Mines Aug 09 '17 at 07:38
  • You might bind `this` and the first parameter with `marker.addListener('click', populateInfoWindow.bind(this, largeInfowindow));` – Nina Scholz Aug 09 '17 at 07:39
  • hi @Mina, i replace this: marker.addListener('click', function(){ populateInfoWindow(this, largeInfowindow); }); with your suggest code: marker.addListener('click', populateInfoWindow.bind(this, largeInfowindow)); and it works! Thanks a lot! Can i ask you something else, that maybe is easy for you but complicated for me? – Mines Aug 09 '17 at 07:47
  • @Mines if you wish to tag someone in comment, use *@* followed my name. That way, necessary person gets notification and this also prevents typos in name. :-p – Rajesh Aug 09 '17 at 07:50
  • in the same project there is a small issue with infowindow, which cant figure out, when i click the marker infowindow works fine as well as when i close it. The problem is when i reopen it that the wikipedia error pops up, which it shouldn't. Any idea how to fix it? – Mines Aug 09 '17 at 07:51
  • @Mines, actually not. – Nina Scholz Aug 09 '17 at 08:01
  • thank you guys for your help! – Mines Aug 09 '17 at 08:02
  • @Rajesh hi again!just notice that what Nina suggest marker.addListener('click', populateInfoWindow.bind(this, largeInfowindow)); works fine from the list but markers are broken. They cant display infoWindows any more. Any help guys? https://github.com/Heleni/Google_Maps/blob/master/script/app2W.js What Project need is when click a title from the list an infowindow pops up but also when user clicks on the marker an infowindow to display – Mines Aug 09 '17 at 10:34

0 Answers0