【问题标题】:Defining variable in strict mode在严格模式下定义变量
【发布时间】:2016-07-11 09:22:01
【问题描述】:

我尝试对我的流星应用脚本使用严格模式。但是使用这段代码:

  "use strict";

  example = function(param) {
      var userId = Meteor.userId();
  }

给了我两个 linter 错误:

  'Meteor' is not defined.
  'example' is not defined.

对于最后一个,我认为我必须将函数定义为

  const example = function() {}

更新

然后 linter 给了我错误example is defined, but never used。我在一个单独的文件中定义了一些函数,所以这个函数不会在这个文件中使用。

同样的问题:

var month = parseInt(moment().format('MM'));

moment() 没有定义...

【问题讨论】:

  • window.Meteor = Meteor 怎么样?然后将严格模式限制在函数范围内?
  • 但这会导致代码很糟糕,不是吗?
  • 在我的项目中,我使用上述方法,即“使用严格”来发挥作用域。否则我将无法使用任何库。
  • 将流星定义为 linter 的全局变量。

标签: javascript meteor strict


【解决方案1】:

linter 不知道 Meteormoment,因为它们是由其他脚本定义的。

您如何处理取决于您使用的是哪种 linter。

/* global Meteor, moment */ 放在脚本顶部可能会解决它。

【讨论】:

  • 哦,不知道。
  • 请查看更新后的帖子,了解最后一个问题。
猜你喜欢
  • 1970-01-01
  • 2015-10-11
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
  • 2021-10-04
  • 1970-01-01
  • 2018-10-02
相关资源
最近更新 更多