2

在 JSHint 中运行此代码时出现几个“未定义”错误:

MERLIN.namespace('MERLIN.http');

MERLIN.http = function ($, window) {
    'use strict';
    // import dependencies

    function request(config) {
        if (!config || typeof config !== 'object') {
            return;
        }
        // perform request
        $.ajax({
            type: config.type || 'GET',
            url: config.url,
            dataType: config.dataType,
            data: config.data || {},
            processData: config.process || false,
            beforeSend: function () {
                indicator(config.panel, config.indicator);
            },
            complete: function () {
                indicator(config.panel, config.indicator);
            },
            success: function (resp) {
                var callback = config.success || null;
                if (typeof callback !== 'function') {
                    callback = false;
                }
                if (callback) {
                    callback.apply(this, [resp]);
                }
            },
            error: function (xhr, textStatus, errorThrown) {
                httpError({
                    xhr: xhr,
                    status: textStatus,
                    error: errorThrown,
                    panel: config.panel
                });
            }
        });
    };

    function indicator(panel, type) {
        if ((!panel || typeof panel !== 'string') || (!type || typeof type !== 'string')) {
            return;
        }
        var indicatorType = (type === 'large') ? type = 'indicatorLarge' : type = 'indicatorSmall';
        return $(panel).toggleClass(indicatorType);
    };

    function httpError() {
        return this;
    };

    return {
        request: request,
        error: httpError
    };

} (jQuery, this);

我不确定为什么会为“indicator”和“httpError”抛出未定义的错误,以及为什么使用“return this”是潜在的严格违规行为。我知道我可以放心地忽略与命名空间相关的未定义错误,因为通用命名空间函数是在前面的单独文件中定义的。

这只是实用主义与严格验证的例子吗?

谢谢 :)

4

1 回答 1

5

关于'indicator' is not defined.和类似的错误:JSHint 派生自 Douglas Crockford 编写的 JSLint。Crockford 有一个关于在定义之前出现在源文本中的函数的调用,即使它是完全正确和合法的代码,并且没有任何歧义。我实际上认为这是 JSLint(以及启用该错误时的 JSHint)积极反帮助的情况——我想知道什么时候真正未定义,而不是根据 Crockford 的风格规则未定义。(并不是说我有意见。)

您可以通过将 的声明移到上面来避免这些错误indicator,但是除了来自 JSHint 的错误错误之外,没有理由这样做。httpErrorrequest

关于 上的错误return this;,我相信这是 JSLint/JSHint 告诉您正在返回全局对象的方式,因为它期望以小写字母开头的函数仅被称为函数而不是伪方法。为什么httpError返回this?您调用它的方式this将是全局对象。

因此,尽管在这种情况下您返回全局对象是正确的,但您也可以完全虚假地得到该错误。例如,此代码会产生该错误:

var Foo = (function() {
    "use strict";

    function Foo() {

    }
    function bar() {
        return this; // "ERROR: [8:16]: Strict violation."
    }
    Foo.prototype.bar = bar;

    return Foo;
})();

那里没有任何严格的违规行为。bar将返回this,如果我bar正确调用(例如,var f = new Foo(); f.bar();),它将是通过创建的对象的实例Foo,而不是全局对象。

如果我更改该代码以便我不帮助我的工具帮助我

var Foo = (function() {
    "use strict";

    function Foo() {

    }

    Foo.prototype.bar = function() {
        return this;
    };

    return Foo;
})();

...错误消失了,因为 JSLint/JSHint 假定将调用该函数并将其this设置为全局对象以外的其他对象。但是我的函数是匿名的,这不太理想。

但是你可以通过让函数名以非小写字母开头来让 JSLint/JSHint 满意。例如,我通常的命名约定有效:

var Foo = (function() {
    "use strict";

    function Foo() {

    }
    function Foo$bar() {
        return this;
    }
    Foo.prototype.bar = Foo$bar;

    return Foo;
})();

没有产生错误。名称Foobar,Foo_bar$bar所有的工作也一样。

于 2012-01-18T09:42:44.733 回答