4

我正在尝试构建一个简单的 JavaScript 验证器,例如JSHint。我想编写一个 JavaScript 代码并单击“验证”按钮,错误应该会显示出来。

像这样 JSHINT(js-source) 传递 JavaScript 源代码只会给你“真”或“假”,具体取决于代码是否有效。我想获取每个错误的详细信息(例如发生错误的行号、错误类型等)。

这是我已经拥有的。

http://jsbin.com/AZEpOHi/1/edit

4

1 回答 1

3

From the docs:

If your input passes JSHint tests, the function will return true. Otherwise, it will return false. In that case, you can use JSHINT.errors to retrieve the errors or request a complete report by calling the JSHINT.data() method.

The errors are in the JSHINT.errors array, and those objects include line numbers and error reasons.

Here's a jsfiddle that demonstrates a simple use of JSHINT.errors.

于 2013-09-20T00:28:15.967 回答