JSHint 错误“对象不支持此属性或方法”用于 javascript 闭包

JSHint error "Object doesn't support this property or method" for javascript closures

本文关键字:方法 用于 javascript 闭包 属性 错误 对象 不支持 JSHint      更新时间:2023-09-26

我有这个javascript:

(function ($) {
    // a comment
})(window.jQuery);

我正在使用以下选项运行 JSHint 的 wsh 版本:

命令:

%systemroot%'system32'cscript.exe

参数:

//Nologo "%userprofile%'Documents'jshint'env'wsh.js" /forin:true,noarg:true,noempty:true,eqeqeq:true,bitwise:true,undef:true,unused:true,browser:true,jquery:true,indent:4,maxerr:500 $(ItemPath)

$(ItemPath)将替换为 Visual Studio 中当前选定的项)

。它给了我这个输出:

[%pathname%'js'JScript1.js]
Line 3 character 18: Object doesn't support this property or method
Implied globals:
    window: 3

我尝试安排括号以使用 JSLint 样式,但这给了我同样的错误:

(function ($) {
    // a comment
}(window.jQuery));

我是否对我的javascript做错了什么,或者这是一个JSHint错误?

更新:我倾向于JSHint错误,这个javascript:

(function ($) {
    // a comment
})(window.jQuery);
// is this a bug?
window.alert("maybe?");

给我这个错误:

Line 5 character 23: Object doesn't support this property or method

更新#2:我认为这是将参数传递给wsh.js的正确方法:

//U //Nologo "%userprofile%'Documents'jshint'env'wsh.js" /forin:true /noarg:true /noempty:true /eqeqeq:true /bitwise:true /undef:true /unused:true /browser:true /jquery:true /indent:4 /maxerr:500 $(ItemPath)

但是,此代码:

(function (w, $) {})(window, jQuery);

仍然输出这个:

[%pathname%'js'JScript1.js]
Line 1 character 37: Object doesn't support this property or method

更新#3:事实证明,我确实是个白痴。

我试图使用最新的:https://nodeload.github.com/jshint/jshint/zipball/master

。当我应该使用 r09 时:https://nodeload.github.com/jshint/jshint/zipball/r09

所以这是最新 master 中的一个错误,并且已在 r10 版本中修复:http://www.jshint.com/changelog/

您不希望通过自实例化匿名函数将window.jQuery作为参数传递,而是window, jQuery。对象window没有方法jQuery,因此错误。