1
/*global
    test: true,
    equal: true,
*/

(function () {
    "use strict";
    test("each() and internals", function () {
        var globals = [
                new Array(),
                new Object(),
                new String(),
                new Number(),
                new Function(),
                new Boolean(),
                new Date(),
                new RegExp()
            ],
            ...

我正在编写一些 QUnit 测试,我想通过 jslint。我想使用一组全局对象,因为它们与它们的字面表示明显不同。

除了最后两个之外,Jslint 不喜欢其中任何一个。我没有看到放松 jslint 肛门的选项。

是的,我希望我的功能测试通过 jslint(而不是 jshint)。是的,我想在某些测试中使用 Object Constructor 而不是文字。

失败的测试

Use the array literal notation [].

                new Array(),

line 31 character 21
Use the object literal notation {} or Object.create(null).

                new Object(),

line 32 character 21
Do not use String as a constructor.

                new String(),

line 33 character 21
Do not use Number as a constructor.

                new Number(),

line 34 character 29
The Function constructor is eval.

                new Function(),

line 35 character 21
Do not use Boolean as a constructor.

                new Boolean(),
4

3 回答 3

2

来自 JSLint 指令

http://www.jslint.com/lint.html

JSLint 不希望看到包装形式 new Number、new String、new Boolean。

JSLint 不希望看到新的对象。请改用 {}。

JSLint 不希望看到新的 Array。请改用 []。

看起来没有选项可以控制它

于 2013-07-08T15:01:14.317 回答
1

此行为由switchJSLint 源代码中的以下语句控制:

switch (c.string) {
case 'Object':
    token.warn('use_object');
    break;
case 'Array':
    if (next_token.id === '(') {
        // ...
        if (next_token.id !== ')') {
            // ...
        } else {
            token.warn('use_array');
        }
        // ...
    }
    token.warn('use_array');
    break;
case 'Number':
case 'String':
case 'Boolean':
case 'Math':
case 'JSON':
    c.warn('not_a_constructor');
    break;
case 'Function':
    if (!option.evil) {
        next_token.warn('function_eval');
    }
    break;
case 'Date':
case 'RegExp':
case 'this':
    break;
default:
    // ...
}

如您所见,除了Function构造函数的情况外,没有选项可以打开或关闭这些检查。evil可以通过将选项设置为 来关闭该警告true

Array在您的情况下,您可以安全地将对and构造函数的调用替换为Object对应的字面量。对于其他警告,您别无选择,只能忽略它们。

于 2013-07-08T15:06:33.267 回答
1

Jslint 怀疑您是否真的想这样做,因为使用构造函数会使比较意外失败:

var s1 = new String('hi');
var s2 = new String('hi');

// but s1 == s2 is false now!  Not to mention 'hi' === s1!

更多细节:http: //jslinterrors.com/do-not-use-a-as-a-constructor/

于 2013-07-08T14:57:24.690 回答