jsLint仍然给出警告“X在被定义之前被使用过”,即使undef被设置(jsLint still giving warning “X was used before it was defined.”

编程入门 行业动态 更新时间:2024-10-27 17:12:52
jsLint仍然给出警告“X在被定义之前被使用过”,即使undef被设置(jsLint still giving warning “X was used before it was defined.” even when undef is set)

最近js文件中的拼写错误导致我的一个项目出现问题,但仅限于某些浏览器。 我试图通过jsLint运行我们大部分的js文件来查找其他未检测到的问题。 我们的目标是让我们所有的js文件通过jsLint或者通过纠正问题或者如果我对问题确定,然后打开适当的jsLint选项。

如果我能做到这一点,那么将来我希望找到一种更经常运行jsLint的方法,但首先我需要让所有文件都通过。

尽管如此,我遇到了很多麻烦。 我不断收到“X在被定义之前使用过”的警告/错误。 我很喜欢在定义它们之前使用物品,所以我想关闭此警告。 我试着在顶部的注释中设置undef:true,并通过Web界面设置复选框,但截至今日,我仍然收到此错误。

这个选项是否被破坏,或者我做错了什么或错误理解这个选项的作用。

根据文档 ,这应该在功能上工作。

我目前使用jsLint 网络工具扫描下面的代码。

/*jslint undef: true, white: true, browser: true */ /*global jQuery */ (function($){ "use strict"; $(function() { createDashboard(); loadDashboardDataFromControls(); }); function loadDashboardDataFromControls() { } function createDashboard() { } }(jQuery));

recently a typo in a js file caused a problem on one of my projects, but only on certain browsers. I'm trying to run most of our js files through jsLint to look for any other un-detected problems. The goal is to get all of our js files to pass jsLint either by correcting the issue or if I'm ok with the issue then turning on the appropriate jsLint option.

If I can do that, then in the future I would like to find a way to run jsLint more often, but first I need to get all the files passing.

I'm having a lot of trouble with one issue though. I keep getting the warning/error that "X was used before it was defined." I'm ok with using items before they are defined, so I wanted to turn this warning off. I've tried setting undef: true in the comment at the top and setting the checkbox through the web interface but as of today, I'm still getting this error.

Is this option broken or am I doing something wrong or mis-understanding what this option does.

According to the docs, this should work on functions.

I'm currently using the jsLint web tool to scan the code below.

/*jslint undef: true, white: true, browser: true */ /*global jQuery */ (function($){ "use strict"; $(function() { createDashboard(); loadDashboardDataFromControls(); }); function loadDashboardDataFromControls() { } function createDashboard() { } }(jQuery));

最满意答案

根据JSLint源代码 (截至撰写本文时的最新提交), undef在严格模式下被认定为false :

function use_strict() { if (next_token.string === 'use strict') { if (strict_mode) { warn('unnecessary_use'); } edge(); advance(); semicolon(); strict_mode = true; option.undef = false; return true; } return false; }

正如我所假设的你所知道的,你可以通过重新安排你的源代码来轻松避免这种警告。 只需在$([...])之上移动loadDashboardDataFromControls()和loadDashboardDataFromControls() $([...]) 。

注意:在严格模式下 ,变量需要在使用前定义,你的代码显然违反该规则。 因此,在我看来,我们所看到的至少是一个文档错误:JSLint文档应该声明,在严格模式下, undef标志设置为false 。

According to the JSLint source code (latest commit as of this writing), undef is sef to false in strict mode:

function use_strict() { if (next_token.string === 'use strict') { if (strict_mode) { warn('unnecessary_use'); } edge(); advance(); semicolon(); strict_mode = true; option.undef = false; return true; } return false; }

As I assume you are aware of, you could easily avoid the warning by rearranging your source code. Simply move loadDashboardDataFromControls() and loadDashboardDataFromControls() above $([...]).

Note: While in strict mode, variables need to be defined before being used, your code obviously does not violate that rule. So, in my opinion, what we see is at least a documentation bug: JSLint documentation should state that in strict mode, the undef flag is set to false.

更多推荐

本文发布于:2023-07-24 08:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1243474.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:使用过   定义   undef   jsLint   defined

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!