【问题标题】:Should I worry about these JSLint warnings on HTML5Shiv.min.js version 3.7.2?我应该担心 HTML5Shiv.min.js 版本 3.7.2 上的这些 JSLint 警告吗?
【发布时间】:2015-05-24 12:56:12
【问题描述】:

我从Cloudflare 链接复制了一个 HTML5Shiv.min.js 版本,当我将文件导入 Adob​​e Brackets 时,JSLint 编译器告诉我脚本包含以下错误:

4   Missing 'use strict' statement.
    !function (a, b) {function c(a, b) {var c = a.createElement("p"), d = a.getElementsByTagName("head")[0] || a.documentElement; return c.innerHTML="x<style>"+b+"</style>",d.ins

4   'c' is already defined.
    !function (a, b) {function c(a, b) {var c = a.createElement("p"), d = a.getElementsByTagName("head")[0] || a.documentElement; return c.innerHTML="x<style>"+b+"</style>",d.ins

4   Expected ';' and instead saw '='.
    !function (a, b) {function c(a, b) {var c = a.createElement("p"), d = a.getElementsByTagName("head")[0] || a.documentElement; return c.innerHTML="x<style>"+b+"</style>",d.ins

4   Unreachable '=' after 'return'.
    !function (a, b) {function c(a, b) {var c = a.createElement("p"), d = a.getElementsByTagName("head")[0] || a.documentElement; return c.innerHTML="x<style>"+b+"</style>",d.ins

4   Expected an identifier and instead saw '='.
    !function (a, b) {function c(a, b) {var c = a.createElement("p"), d = a.getElementsByTagName("head")[0] || a.documentElement; return c.innerHTML="x<style>"+b+"</style>",d.ins

4   Stopping. (100% scanned).
    !function (a, b) {function c(a, b) {var c = a.createElement("p"), d = a.getElementsByTagName("head")[0] || a.documentElement; return c.innerHTML="x<style>"+b+"</style>",d.ins

如果我决定使用它,这真的会起作用吗?缺少语句和无法访问的运算符的代码。也许 JSLint 不是最新的或者有些东西已经关闭,但如果可能的话,我想获得第二个意见。

谢谢。

【问题讨论】:

  • 当然可以——如果没有,你真的认为 HTML5Shiv 脚本会变得那么流行吗?您需要意识到的是,linter 是一种提醒您潜在问题的工具,这些问题不一定是真正的错误
  • 好吧,我明白了,但对我来说,吸引大众还不够。我只是想了解发生了什么。不过谢谢。我刚刚开始学习 JScript,其中一些错误听起来像是用其他语言无法编译的东西!
  • 好吧,对大众有用的东西至少不会那么错 ;-) 当涉及到这种脚本时,Web 开发人员社区通常非常投入——如果有人提出这样的解决常见问题的解决方案,然后由其他人审查并在必要时进行改进。
  • 同样,此类 linter 工具的主要目的是提醒潜在问题,并按照“更严格”的标准编写脚本。他们经常会说,“这看起来可能会导致问题”,但这并不一定意味着实际上存在这样的错误。它们是编写“更干净”代码的一种方式,但不一定是比开始时“更好”的代码。编写这样的脚本的人可能会为问题寻求“更肮脏”的解决方案,只是为了让脚本更小。

标签: javascript jslint adobe-brackets html5shiv


【解决方案1】:

别着急。你可以使用图书馆。唯一的“缺失”行是"use strict";,这是 JSLint 喜欢的东西,但您不需要使用它。 (Here's a decent discussion of use strict.)

JSLint 和其他 linter 会寻找两种类型的“错误”:functionalstyle。 JSLint 发现的许多样式错误实际上会转化为您想要避免的逻辑错误。这是一个很棒的工具。

同时,您可以在不破坏代码功能的情况下产生 JSLint 不喜欢的样式错误,尤其是在缩小代码中。当您看到 JSLint 识别第三方代码或任何人的缩小代码中的样式错误时,请不要担心。它们不一定是功能问题。

通常,您应该免除 3rd 方库的 linting。对于外部库,你无能为力,除非你想自己 fork 和 lint,这太疯狂了。 ;^) 而且,缩小代码通常采用对 lint 不友好的快捷方式。在您缩小代码之前检查 您的 代码以保持其高质量,但不要担心您不应该接触的 QAing 库。假设他们有另一种确保高质量的方法,这可能包括使用不同的 linter,或具有不同规则集的 linter。

这是一个“肮脏”的秘密......

jQuery 也很讨厌 JSLint...

jQuery, even unminified,例如,也不 lint。即使我添加一行来告诉 JSLint 取消空格“错误”,缺少“使用严格”,并让它知道它应该假设一个浏览器,我得到......

'module' was used before it was defined.
    if ( typeof module === "object" && typeof module.exports === "object" ) {
line 18 character 44'module' was used before it was defined.
    if ( typeof module === "object" && typeof module.exports === "object" ) {
line 26 character 3'module' was used before it was defined.
        module.exports = global.document ?
line 39 character 3Unexpected 'typeof'. Use '===' to compare directly with undefined.
}(typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
line 49 character 5 Combine this with the previous 'var' statement.
    var slice = deletedIds.slice;
line 51 character 5 Combine this with the previous 'var' statement.
    var concat = deletedIds.concat;
line 53 character 5 Combine this with the previous 'var' statement.
    var push = deletedIds.push;
line 55 character 5 Combine this with the previous 'var' statement.

等等等等。没有人会争辩说 jQuery 是假的,你知道吗?所以不要担心 Cloudfire 或任何其他文件是否会给你同样的错误集。

底线:不要对图书馆大喊大叫,尤其是缩小的图书馆。 Linter 是 您的 代码的代码质量工具。如果其他人有其他方法来保持他们的代码正常工作并且它为您的使用进行了很好的测试,请不要理会 lib。 ;^)

【讨论】:

    猜你喜欢
    • 2016-08-08
    • 2019-07-13
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    相关资源
    最近更新 更多