简体   繁体   English

JSLint(Javascript Validator网站) - 错误! 隐含的全球:

[英]JSLint (Javascript Validator Website) - Error! Implied global:

I just tested my custom gallery script at JSLint.. and all errors where solved except for one. 我刚刚在JSLint上测试了我的自定义图库脚本..除了一个之外,所有错误都解决了。 The implied global error.. Is this really an error? 隐含的全局错误..这真的是一个错误吗? Can I ignore it or should I work on it to solve this error..? 我可以忽略它,还是应该用它来解决这个错误..?

Thank you for your responses! 谢谢您的反馈!

替代文字

Error:
Implied global:
<bunch of vars and other stuff i dont know>

What does this mean? 这是什么意思? BTW I use JQuery Library.. maybe thats the problem^^.. 顺便说一句,我使用JQuery库..也许这就是问题^^ ..

if you use externally declared variables like in this case, put a 'global' statement at the top of your file, like this: 如果您使用外部声明的变量(例如在这种情况下),请在文件顶部放置一个“全局”语句,如下所示:

/*global $, document */ / * global $,document * /

JSLint documentation says: JSLint文档说:

Undefined Variables and Functions 未定义的变量和函数

JavaScript's biggest problem is its dependence on global variables, particularly implied global variables. JavaScript最大的问题是它依赖于全局变量,特别是隐含的全局变量。 If a variable is not explicitly declared (usually with the var statement), then JavaScript assumes that the variable was global. 如果未显式声明变量(通常使用var语句),则JavaScript假定变量是全局变量。 This can mask misspelled names and other problems. 这可以掩盖拼写错误的名称和其他问题。

JSLint expects that all variables and functions are declared before they are used or invoked. JSLint期望在使用或调用所有变量和函数之前声明它们。 This allows it to detect implied global variables. 这允许它检测隐含的全局变量。 It is also good practice because it makes programs easier to read. 这也是一种很好的做法,因为它使程序更容易阅读。

Care for that error. 关心那个错误。 Nearly every coding convention wants you not to use implied globals. 几乎每个编码约定都要求你不要使用隐含的全局变量。

Variables can be declared using the var keyword. 可以使用var关键字声明变量。

When writing JavaScript code for a browser, it is useful to indicate to JSLint that you are in browser mode, such as by including this: 在为浏览器编写JavaScript代码时,向JSLint指示您处于浏览器模式是很有用的,例如包括:

/*jslint browser: true */

This should resolve 'document', 'setTimeout', and other typical browser defaults 这应该解析'document','setTimeout'和其他典型的浏览器默认值

Since jQuery is probably not being evaluated in the same context as your JavaScript, you'll need to let it be known that the ever-useful '$' is available with: 由于jQuery可能没有在与JavaScript相同的上下文中进行评估,因此您需要知道有用的“$”可用于:

/*global $ */

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

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