简体   繁体   English

如何在没有ESLint no-unused-var错误的情况下公开全局javascript函数?

[英]How do you expose a global javascript function without ESLint no-unused-var error?

The following code is valid in ESLint with Google's style guide with one exception; 以下代码在ESLint中与Google的样式指南一起使用,但有一个例外; the closure function Counter gets a no-unused-vars error when the script is checked using ESLint. 使用ESLint检查脚本时,闭包函数Counter获取no-unused-vars错误。

/**
 * Create a counter that is incremented and returned when called
 * @return {object} - incrementor function
 */
function Counter() {
  var _i = 0;

  /**
   * increment counter
   * @return {int} - The incremented integer
   */
  function _incrementor() {
    _i++;
    return _i;
  }

  _incrementor.incr = function() {
    this.call();
    return _incrementor;
  };

  _incrementor.val = function(val) {
    if (!arguments.length) { return _i; }
    _i = val;
    return _incrementor;
  };

  return _incrementor;
}

I would like to have this function (or one structure the same way) as a standalone script that I can include in my HTML and then call from a different script like so: 我想将这个函数(或一个结构以相同的方式)作为一个独立的脚本,我可以包含在我的HTML中,然后从不同的脚本调用,如下所示:

var count = Counter()
    .val(5);

count.incr() 
console.log(count.val())  // prints => 6

I have tried including /* exported Counter */ at the top of the script but the error persists. 我已经尝试在脚本的顶部包含/* exported Counter */但错误仍然存​​在。 How do I silence/fix this error? 如何静音/修复此错误?

Explicitly adding Counter to the global scope silences this error and prevents errors that may occur as a result of the implicit global scope used in the question. 明确地将Counter添加到全局范围会使此错误无效并防止由于问题中使用的隐式全局范围而可能发生的错误。

 /** * Create a counter that is incremented and returned when called * @return {object} - incrementor function */ this.Counter = function() { var _i = 0; /** * increment counter * @return {int} - The incremented integer */ function _incrementor() { _i++; return _i; } _incrementor.incr = function() { this.call(); return _incrementor; }; _incrementor.val = function(val) { if (!arguments.length) { return _i; } _i = val; return _incrementor; }; return _incrementor; }; 

Here's some options for telling the linter to allow a global Counter variable: 这里有一些选项可以告诉linter允许全局Counter变量:

Option #1 : Add this comment to the top of the js file when you need to use the global variable: 选项#1 :当您需要使用全局变量时,将此注释添加到js文件的顶部:

/* globals Counter */

Option #2: Add the variable name to the globals property in your eslint configuration file: 选项#2:将变量名称添加到eslint配置文件中的globals属性:

// eslintrc.js

module.exports = {
  // ...

  globals: {
    'Counter': true
  }
}

See ESLint documentation here for more info. 有关详细信息,请参阅此处的ESLint文档

Note: you can also use the env property in your config file for predefined sets of globals like: browser (ie. localStorage), jquery, node, etc.). 注意:您还可以在配置文件中使用env属性来预定义的全局变量集,例如:browser(即localStorage),jquery,node等)。 See here . 看到 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM