【问题标题】:"Read only" JSLint Error“只读”JSLint 错误
【发布时间】:2016-12-07 19:34:31
【问题描述】:

我正在尝试设置谷歌地图地理位置,而 JSLint 一直说“只读”。这段代码:

function geolocationSuccess(position) {
    // Turn the geolocation position into a LatLng object.
    location = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);

    // Map that point.
    map.setCenter(location);
}

【问题讨论】:

  • 您有问题吗?
  • 可能是在抱怨你设置window.location
  • @Jordan 是的,为什么 JSLint 会这么说,我该如何解决
  • @Srikanth 所以我会写同样的东西? /*全局arrayContainer:true*/

标签: javascript jslint


【解决方案1】:

您是否尝试过使用var location 而不是location?见下文

function geolocationSuccess(position) {
  // Turn the geolocation position into a LatLng object.
  var location = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);

  // Map that point.
  map.setCenter(location);
}

JSLint 似乎在抱怨无法分配的全局变量。 var location 将使location 成为局部变量而不是全局变量。

【讨论】:

  • 成功了,谢谢!但在书中的示例中,location 之前没有显示var...
  • 嗯...如果它的位置定义为var location OUTSIDE of geolocationSuccess 函数(但在同一个文件中),那么在函数内部你可以只有位置(没有 var)。
  • 我已经尝试过了,但它仍然给了我同样的错误......:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 2012-07-14
  • 2011-08-17
  • 2014-11-30
  • 2012-04-15
  • 2011-09-27
  • 2013-08-10
相关资源
最近更新 更多