简体   繁体   English

JSHint在揭示模块模式中引发“未定义”警告

[英]JSHint throwing 'undefined' warnings in revealing module pattern

I am getting several 'undefined' errors when running this code in JSHint: 在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);

I am not sure why the undefined errors are being thrown for 'indicator' and 'httpError' and why the use of 'return this' is a potential strict violation. 我不确定为什么会为“指标”和“ httpError”抛出未定义的错误,以及为什么使用“返回此值”会潜在地严格违反。 I know I can safely ignore the undefined error relating to the namespace as the general purpose namespace function is defined earlier in a separate file. 我知道我可以放心地忽略与命名空间有关的未定义错误,因为通用命名空间功能是在单独的文件中定义的。

Is it just a case of pragmatism versus strict validation? 这仅仅是实用主义还是严格的验证?

Thanks :) 谢谢 :)

Regarding the 'indicator' is not defined. 关于'indicator' is not defined. and similar errors: JSHint is derived from JSLint, which was written by Douglas Crockford. 和类似的错误:JSHint派生自Douglas Crockford编写的JSLint。 Crockford has a thing about calls to functions appearing in the source text before they're defined, even though it's perfectly correct and legal code and there's no ambiguity whatsoever. Crockford关于在定义之前在源文本中出现的函数的调用,尽管这是完全正确和合法的代码,并且没有任何歧义。 I actually think this is a case of JSLint (and JSHint when that error is enabled) being actively anti-helpful — I want to know when something is truly undefined, not when it's undefined-according-to-Crockford's-style-rules. 我实际上认为这是JSLint(启用该错误时为JSHint)正在积极地提供帮助的情况-我想知道什么时候真正地是未定义的,而不是何时是未定义的(根据Crockford的样式规则)。 (Not that I have an opinion.) (不是我有意见。)

You could avoid those errors by moving the declarations of indicator and httpError up above request , but other than the false errors from JSHint, there's no reason to do so. 你可以通过移动的声明避免这些错误indicatorhttpError了上述request ,但不是从JSHint虚假错误等,没有理由这样做。

Regarding the error on return this; 关于return this;错误 , I believe that's JSLint/JSHint's way of telling you that you're returning the global object, because it expects that functions starting with a lower-case letter are just called as functions and not a pseudo-methods . ,我相信这是JSLint / JSHint告诉您正在返回全局对象的方式,因为它希望将以小写字母开头的函数称为函数,而不是伪方法 Why does httpError return this ? 为什么httpError返回this The way you're calling it, this will be the global object. 你打电话的方式, this将是全球对象。

So although it's right that you're returning the global object in that case, you can also get that error completely spuriously. 因此,尽管在这种情况下返回全局对象是正确的,但您也可以完全虚假地获得该错误。 For instance, this code produces that error: 例如,此代码产生该错误:

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

    function Foo() {

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

    return Foo;
})();

There's no strict violation there whatsoever. 那里没有任何严格的违规行为。 bar will return this , which if I call bar correctly (eg, var f = new Foo(); f.bar(); ) will be an instance of an object created via Foo , not the global object. bar将返回this ,如果我正确调用bar (例如var f = new Foo(); f.bar(); ),它将是通过Foo创建的对象的实例,而不是全局对象。

If I change that code so that I don't help my tools help me : 如果我更改该代码以免我的工具无法帮助我

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

    function Foo() {

    }

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

    return Foo;
})();

...the error goes away, because JSLint/JSHint assume that the function will be called with this set to something other than the global object. ...错误消失了,因为JSLint / JSHint假定将使用this设置将函数调用为全局对象以外的函数。 But then my functions are anonymous, which is less than ideal. 但是我的函数是匿名的,这不理想。

But you can make JSLint/JSHint happy by making the function name start with something other than a lower-case letter. 但是,通过使函数名称以小写字母开头,可以使JSLint / JSHint满意。 For instance, my usual naming convention works: 例如,我通常的命名约定有效:

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

    function Foo() {

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

    return Foo;
})();

No errors generated. 没有错误产生。 The names Foobar , Foo_bar , and $bar all work as well. 名称FoobarFoo_bar$bar都可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM