简体   繁体   English

为什么JSLint会抱怨未定义/隐含的全局变量?

[英]Why does JSLint complain about undefined/implied global variable?

I'm trying to understand why JSLint complains about an implied global variable in the following example: 我试图理解为什么JSLint在以下示例中抱怨隐含的全局变量:

var TEST = (function () {
  var count = 0;

  function get_count() { 
    return add_one(); 
  }

  function add_one() {
    count += 1;
    return count;
  }

  return { 
    get_count: get_count
  };
}());

Running this through JSLint gives the error: 通过JSLint运行它会给出错误:

Problem at line 5 character 12: 'add_one' is not defined. 第5行第12个问题:未定义“add_one”。

As well as saying: 除了说:

Implied global: add_one 5 隐含的全局:add_one 5

If you move the add_one() function before the get_count() function the error goes away. 如果在add_one()函数之前移动get_count()函数,则错误消失。 However with the code as it is above, it doesn't produce any errors when you run it in the browser. 但是,使用上面的代码,在浏览器中运行它时不会产生任何错误。 Can anyone explain why JSLint is complaining? 任何人都可以解释为什么JSLint抱怨?

Thanks! 谢谢!
Matt 马特

This is because JSLint uses a Pratt Parser , a top-down parser, not a full-blown JavaScript interpreter. 这是因为JSLint使用Pratt Parser一个自上而下的解析器,而不是一个完整的JavaScript解释器。 If it were truly interpreted, then it wouldn't give you that error. 如果真的被解释了,那么它就不会给你那个错误。

add_one is an implied global because the parser didn't come across that variable yet, so it assumes that your surrounding code will have that variable. add_one是一个隐含的全局,因为解析器还没有遇到该变量,因此它假定您的周围代码将具有该变量。 But, if you flip it around, then the parser has already came across the add_one variable and it's all peaches and cream :-) 但是,如果你翻转它,那么解析器已经遇到了add_one变量,它是所有的桃子和奶油:-)

By the way, I noticed a small typo in your closing function line: }()); 顺便说一句,我注意到你的结束函数行中有一个小错字: }()); should be })(); 应该是})(); .

I change the order inwhich the methods are declared it will solve your problem. 我改变了声明方法的顺序,它将解决你的问题。 As mentioned in another answer, some JavaScript parses use a top down approach to reading the code, similar to how the C programming language does it. 正如另一个答案中所提到的,一些JavaScript解析使用自顶向下的方法来读取代码,类似于C编程语言的工作方式。 Modern interpreters and compilers use a 2 pass approach. 现代翻译和编译器使用2遍方法。 The first pass is reading/compiling the methods into memory. 第一步是将方法读入/编译到内存中。 If it encounters any method calls that it does not know about it will look through the entire set of methods in memory to determine if it exists. 如果遇到任何它不知道的方法调用,它将查看内存中的整个方法集以确定它是否存在。 I would recommend fixing the order because while it may not cause an issues, it will load the method into memory sooner with the change. 我建议修复订单,因为虽然它可能不会引起问题,但它会在更改后尽快将方法加载到内存中。

var TEST = (function () {
  var count = 0;

  function add_one() {
    count += 1;
    return count;
  }

  function get_count() { 
    return add_one(); 
  }

  return { 
    get_count: get_count
  };
}());

你得到* add_one没有定义*因为JSLint认为函数表达式应该以正确的顺序声明,而另一个问题(“*隐含的全局:add_one 5 *”),你就可以发表评论,例如/ * globals add_one * /在脚本的顶部

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

相关问题 为什么JSLint会在'返回'之后抱怨“意外'其他'”? - Why does JSLint complain about “Unexpected 'else' after 'return'”? 为什么 jslint 在模板字符串上抱怨 Unexpected '`' - Why does jslint complain Unexpected '`' on template strings 为什么打字稿不会抱怨某些未定义的变量 - Why does typescript not complain about certain undefined variables 当我在RegEx中转义<字符时,为什么jslint会抱怨? - Why does jslint complain when I escape the < character in a RegEx? JSLint(Javascript Validator网站) - 错误! 隐含的全球: - JSLint (Javascript Validator Website) - Error! Implied global: 为什么 React Linter 抱怨没有将 const 变量添加到依赖数组中? - Why does React Linter complain about a const variable not being added to the dependency array? 为什么JSLint告诉我使用“=== undefined”而不是“typeof ... ==='undefined'”? - Why does JSLint tell me to use “=== undefined” instead of “typeof … === 'undefined'”? 括号中的JSLint表示未定义音频,但不抱怨图像 - JSLint in Brackets says Audio is not defined, but doesn't complain about Image JSLint抱怨重新定义undefined - JSLint complains about redefining undefined 为什么jshint会抱怨表达式中的换行符? - Why does jshint complain about linebreak within expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM