首页 > 解决方案 > JSLint 换行格式

问题描述

我真的不明白 JSLint 希望我如何格式化这个脚本:

(function ($) {
    "use strict";

    window.isMobileDevice = function () {
        return (window.orientation !== undefined) || (navigator.userAgent.indexOf("IEMobile") !== -1);
    };
}());

我收到此错误:

Line is longer than 80 characters.
        return (window.orientation !== undefined) || (navigator.userAgent.indexOf("IEMobile") !== -1);

如果我添加换行符:

(function () {
    "use strict";

    window.isMobileDevice = function () {
        return (window.orientation !== undefined) ||
            (navigator.userAgent.indexOf("IEMobile") !== -1);
    };
}());

我收到此错误:

Expected '(' at column 8, not column 12.
            (navigator.userAgent.indexOf("IEMobile") !== -1);

换行符应该如何格式化?

标签: javascriptjslint

解决方案


就像它告诉你的那样:Expected '(' at column 8, not column 12.。;^D

将那条线向后移动四个空格,你就是金色的。

这在 JSLint.com 上对我来说是lints

/*jslint browser*/
(function () {
    "use strict";

    window.isMobileDevice = function () {
        return (window.orientation !== undefined) ||
        (navigator.userAgent.indexOf("IEMobile") !== -1);
    };
}());

请注意,我还必须删除该参数$,因为它没有被使用。

但是你可以覆盖它......

我认为这就是我说 JSLint非常固执己见的地方,但幸运的是,这是我们可以将其更改为按您想要的方式工作的地方。如果你设置了“ whitespace mess”指令,你的代码(减去$)就可以了。

/*jslint browser, white*/
(function () {
    "use strict";

    window.isMobileDevice = function () {
        return (window.orientation !== undefined) ||
            (navigator.userAgent.indexOf("IEMobile") !== -1);
    };
}());

推荐阅读