简体   繁体   English

如何让JSLint忽略代码片段?

[英]How to make JSLint ignore pieces of code?

My php file has mainly several javascript functions which I wanted to debug. 我的php文件主要有几个我想调试的javascript函数。 The file starts with: 该文件以:

/*ignore jslint start*/
<? Header("content-type: application/x-javascript");
include('../country files/' . $_GET['country'] . '.php');
?>
/*ignore jslint end*/
  ...
  javascript code

Though, JSLint gives error on line 1! 虽然,JSLint在第1行给出了错误!

You can change JSLint options for a few lines, with comments. 您可以使用注释更改几行的JSLint选项。

For example, JSLint will complain about you using Underscore.JS or Lo-Dash , because it isn't good practice to have underscores at the ends of variables names. 例如,JSLint的会抱怨你使用Underscore.JS罗短跑 ,因为这不是很好的做法,在变量名的结束下划线。 But I can happily use Underscore's each() function by temporarily preventing nomen warnings: 但我可以通过暂时阻止nomen警告来愉快地使用Underscore的each()函数:

/*jslint nomen:true*/ // suppress dangling underscore warnings
_.each( ... ); // example use of Underscore.JS
/*jslint nomen:false*/ // re-enable dangling underscore warnings

However, there isn't a way to use comments to temporarily disable JSLint completely. 但是,没有办法使用注释来完全暂时禁用JSLint。

It is good practice to move your JavaScript out into separate files. 将JavaScript移动到单独的文件中是一种很好的做法。 Ideally, you shouldn't be running JSLint on HTML or PHP files, only on pure JavaScript files. 理想情况下,您不应该在HTML或PHP文件上运行JSLint,只能在纯JavaScript文件上运行。

If you need to dynamically generate JavaScript code with PHP, then run JSlint on a sample of the output, of you like. 如果您需要使用PHP动态生成JavaScript代码,那么就可以在输出样本上运行JSlint。 Generally, you minimise the cases where this is necessary. 通常,您最小化必要的情况。

You could have PHP generate a small amount of JavaScript to establish operational context (eg a few global constants), and leave the rest of your front end functionality to separate pure JavaScript files. 你可以让PHP生成少量的JavaScript来建立操作上下文(例如一些全局常量),并将剩下的前端功能留给单独的纯JavaScript文件。

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

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