最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how can i solve ESLint error that 'no-unused-var' with google map - Stack Overflow

programmeradmin2浏览0评论

I think that it's simple problem but i can't solve it
I had entered /* export myMap */ or /* global myMap */ at the top of the script but errors is continuing

Code

HTML

<h1>My First Google Map</h1>
<div id="googleMap" style="width:60%;height:600px;margin: auto;"></div>
<script src="? key=YOUR_KEY&callback=myMap"></script>

JavaScript

function myMap() {
    var mapProp= {
        center:new google.maps.LatLng(37.540881, 127.079689),
        zoom:17,
    };
    var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
}

When I wrote the code as showed above, I saw the following error

Error ESLint 'myMap' is defined but never used. (no-unused-vars)
Error ESLint 'google' is not defined. (no-undef)
Error ESLint 'map' is assigned a value but never used. (no-unused-vars)
Error ESLint 'google' is not defined. (no-undef)

but google API use 'google', 'map' and I use 'myMap' . it's no doubt
please help beginner. thx

p.s. I don't want solve with /* eslint-disable no-unused-vars */

I think that it's simple problem but i can't solve it
I had entered /* export myMap */ or /* global myMap */ at the top of the script but errors is continuing

Code

HTML

<h1>My First Google Map</h1>
<div id="googleMap" style="width:60%;height:600px;margin: auto;"></div>
<script src="https://maps.googleapis./maps/api/js? key=YOUR_KEY&callback=myMap"></script>

JavaScript

function myMap() {
    var mapProp= {
        center:new google.maps.LatLng(37.540881, 127.079689),
        zoom:17,
    };
    var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
}

When I wrote the code as showed above, I saw the following error

Error ESLint 'myMap' is defined but never used. (no-unused-vars)
Error ESLint 'google' is not defined. (no-undef)
Error ESLint 'map' is assigned a value but never used. (no-unused-vars)
Error ESLint 'google' is not defined. (no-undef)

but google API use 'google', 'map' and I use 'myMap' . it's no doubt
please help beginner. thx

p.s. I don't want solve with /* eslint-disable no-unused-vars */

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 6, 2019 at 11:25 Daniel LeeDaniel Lee 1281 silver badge11 bronze badges 10
  • 2 You can remove var map = in front of the new to solve the map is assigned a value but never used. – t.niese Commented Jan 6, 2019 at 11:29
  • 1 or just return map or if you want more testable function return !!map so it just returns boolean value – Nikko Khresna Commented Jan 6, 2019 at 11:34
  • However you can also put ignore ment /* eslint-disable-next-line */ and for no-undef eg. /*global describe, it, expect*/ – Zydnar Commented Jan 6, 2019 at 11:35
  • how can I resolve 'google' and 'myMap' ? – Daniel Lee Commented Jan 6, 2019 at 11:40
  • 1 If myMap is used in a separated file or script tag, then you have to export myMap so that the linter knows that it will be used somewhere else. If you don't use any bundler/module system then you need to do it by writing window.myMap = myMap – t.niese Commented Jan 6, 2019 at 12:21
 |  Show 5 more ments

1 Answer 1

Reset to default 7

To get rid of the error messages you have to tell your linter that google is an external variable, and you need to tell the linter that you plan to use myMap externally. And as you don't use map later you can just remove the var map =.

The linter is really dumb and can only verify the consistency within the file that it currently lints, it cannot verify that use a function in another file/script and it cannot verify that a variable originates from somewhere else.

The simplest approach would be to use the global option to tell the linter that google was defined somewhere else, and write window.myMap = myMap, that way you explicitly assign myMap to the global object window (which for the given code does not really change anything as myMap is already defined globally) and tells the linter that it is expected that is is used somewhere else outside of that script.

The linter cannot verify that myMap that it is really used somewhere else and has to trust you there.

/* global google */

function myMap() {
  var mapProp = {
    center: new google.maps.LatLng(37.540881, 127.079689),
    zoom: 17,
  };
  new google.maps.Map(document.getElementById("googleMap"), mapProp);
}

window.myMap = myMap;

But if you don't use a module bundler like webpack or rollup and nothing like monjs, amd or native es6 modules, then you should always wrap your code into an IIFE to ensure that you pollute the global scope as less as possible:

(function() {
  /* global google */

  function myMap() {
    var mapProp = {
      center: new google.maps.LatLng(37.540881, 127.079689),
      zoom: 17,
    };
    new google.maps.Map(document.getElementById("googleMap"), mapProp);
  }

  window.myMap = myMap;
}())

And here the window.myMap = myMap has indeed the purpose to make the function available for other scripts.

Instead of writing /* global google */ you could do the inverse of the window.myMap = myMap, but this will only work if the scripts are loaded in the correct order so that the google maps scripts sets goolge before your script executed:

(function() {
  var google = window.google;

  function myMap() {
    var mapProp = {
      center: new google.maps.LatLng(37.540881, 127.079689),
      zoom: 17,
    };
    new google.maps.Map(document.getElementById("googleMap"), mapProp);
  }

  window.myMap = myMap;
}())

Now you have a syntax that is really close to how AMD modules (like used in nodejs or webpack) look like, and would make it easier if you plan to switch to a bundler in future:

var google = require('google');

function myMap() {
  var mapProp = {
    center: new google.maps.LatLng(37.540881, 127.079689),
    zoom: 17,
  };
  new google.maps.Map(document.getElementById("googleMap"), mapProp);
}

exports.myMap = myMap;
发布评论

评论列表(0)

  1. 暂无评论