在 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”是潜在的严格违规行为。我知道我可以放心地忽略与命名空间相关的未定义错误,因为通用命名空间函数是在前面的单独文件中定义的。
这只是实用主义与严格验证的例子吗?
谢谢 :)