0
if (typeof laMap !== "undefined" && laMap !== null && laMap != undefined) {
  laMap.remove();
  let laMap;
} 

I get into the if statement, i.e., true. The laMap in console at the if statement reads in part: laMap 16:38:32.688 e {options: {…}, _container: div#map.leaflet-container.leaflet-touch.leaflet-retina.leaflet-fade-anim.leaflet-grab.leaflet-touch-…, _leaflet_id: 2, _containerId: 3, _fadeAnimated: true, …}. Step in to next function call, i.e., the second line, laMap is laMap 16:40:21.256 undefined. How can this be?

This is part of a larger debugging problem with jS for me. So I've been working through with lots of console.logs and stepping through code. In fact I don't even think I should be getting to this step, but I had to start somewhere.

I see many suggestions and recommendations to consider other title, but mine seems even simpler than any of the others.

Greg
  • 2,359
  • 5
  • 22
  • 35
  • Does the problem go away if you remove the `let laMap;` line? That variable will be hoisted to the top of the scope (the `if` block) and hide the, assumed, previously defined `laMap` variable. – Marc Baumbach Jan 11 '18 at 00:56
  • Yes. In fact I changed` let laMap` to `laMap` after posting and it got by that problem. Thanks. I had gotten sloppy with using `let` there. Was trying to fix a related problem. Now to dig in deeper to my jS flow. Appreciate being given some understanding of why it was happening. Doesn't make sense to me why it isn't processed in order. – Greg Jan 11 '18 at 00:59
  • @Greg Variables in JS are always "hoisted" and apply to their whole scope. It's not like they are "processed in order" and get created when the declaration is encountered. – Bergi Jan 11 '18 at 01:04
  • @Bergi. Thanks I see. @Marc Baumbach. The problem is that if I remove the `let laMap` I get another error and if I don't do the `laMap.remove();` I later get an error when I try to redefine `laMap` as follows `Uncaught Error: Map container is already initialized.` The `remove` solution was offered here on SO. Why I can't see why `laMap = L.map('map' etc` gives an error is beyond me. But this is another question. – Greg Jan 11 '18 at 01:11
  • @Greg Yes, and in that other question you should share your full code. – Bergi Jan 11 '18 at 01:17
  • @Bergi. I will after looking up more about what exactly `remove();` does. – Greg Jan 11 '18 at 01:19
  • The basic problem that I was trying to solve was solved by keeping turbolinks away from any Leaflet related pages. https://stackoverflow.com/questions/27322432/ruby-on-rails-leaflet-rails-not-loading and https://gis.stackexchange.com/questions/215460/can-not-get-leaflet-map-to-show-via-link-in-rails-4 talk to the issue. – Greg Jan 12 '18 at 06:05

0 Answers0