js 对象的属性分配上的 lint 错误
lint error on property assignment for js object
使用此模式lint会在我设置新的公共var(property?)而不给它默认值的情况下抱怨。 这是不好的做法吗?
文件1.js
/*jslint browser: true, white: true, todo: true */
if( typeof(app) === "undefined" ){ //lint complains here about the "undefined"
var app = {};
}
//Self-Executing Anonymous Function (Public & Private)
(function( core, $, undefined ) {
//private var
var privateVarValue;
//public var
app.core.publicVarValue; //lint complains here (expected assignment or function call)
app.core.someFunction = function(){
//do something
});
}( window.app.core = window.app.core || {}, jQuery ));
//文件2.js
/*jslint browser: true, white: true, todo: true */
if( typeof(app) === "undefined" ){ //lint complains here about the "undefined"
var app = {};
}
//Self-Executing Anonymous Function (Public & Private)
(function( aModule, $, undefined ) {
app.aModule .someFunction = function(){
if(someCondition){
app.core.publicVarValue = "1";
}
else {
app.core.publicVarValue = "2";
}
});
app.aModule.a = function(){
//do something
console.log(app.core.publicVarValue);
});
app.aModule.b = function(){
if(app.core.publicVarValue){
app.aModule.a();
} else {
//the public still hasnt been set, kick back an error
console.log('an error occurred');
}
});
}( window.app.aModule= window.app.aModule || {}, jQuery ));
Lint 不喜欢publicVarValue
为空,这让我不得不设置默认值。 有人可以告诉我如何既可以压制 lint 以及我可以应用于此公共 var 以初始化它的最短实际值是多少,它将在以后使用。
编辑,修改问题..
坦率地说,这段代码有点混乱(请参阅下面的项目符号内容),我仍然不确定问题到底是什么。 我将假设它是"我如何检查此代码"。 这是一种方法。
请注意,在我的评论中,我的意思是将您的代码带到 JSLint.com 并让它在那里工作,而无需打开任何开关(将它们拍在文件顶部,就像您开始做的那样;这很好)。 代码中仍然存在许多明显的错误,例如每个函数分配上的额外结尾括号。
[编辑:删除了以前的代码,替换为下面的内容。如果有用,您可以在编辑历史记录中看到旧内容]
所以这里有一些提示:
- 您有范围界定问题。如果您将
window.app.aModule
作为aModule
传入,则没有理由继续在函数中使用全局句柄app.aModule
。(我从这些引用中删除了全局上下文。 编辑:如果你想做匿名关闭技巧,那么 1.)不要传入全局和 2.)确保你在某个地方有一个私人 var,否则你就是在浪费这个把戏! - 函数声明中的结尾部分 - 我将假设这是一个粗心的错误,但是如果你打算让它做某事,它是什么?
- 请注意,为了让 JSLint 满意,你需要使用你定义的东西,并定义你使用的东西——必须使用函数中的参数,并且必须定义像
someCondition
这样的变量。 - 不知道为什么在函数中将
undefined
作为参数 - 那里的目标是什么?...
编辑:好的,我想我明白你现在想做什么;感谢博客链接。 让我失望的是,你没有利用模式的真正优势,即"限制对闭包中变量和函数的访问,基本上使它们私有,只选择将你选择的API公开给全局范围。 一切都附着在你的东西里aModule
;没有"私有"变量。
我想你正在尝试做这样的事情...
/*jslint browser: true, white: true, todo: true, sloppy:true */
/*global jQuery */
// No reason to pass in a globally scoped variable if
// you're always accessing it globally. See Dalgleish's
// line where he says `window.baz = baz;` -- `baz` isn't
// passed.
(function($) {
// Now the way you had this written, it didn't benefit
// from the pattern. Let's use publicVarValue as a
// private variable here, just as an example.
// It's now *only* in this anonymous scope.
var publicVarValue;
// Set up the first function on the global module.
// Note that you never initialized `someCondition`;
// I'm making it a param
window.app.aModule.someFunction = function(someCondition){
if(someCondition){
publicVarValue = "1";
}
else {
publicVarValue = "2";
}
};
// Set up the second function on the global module.
window.app.aModule.a = function(){
//do something
$("#someId").append("Value is: " + publicVarValue + "<br />");
};
// Now we set up the function contingent on publicVarValue
// being set.
window.app.aModule.b = function(){
if (publicVarValue) {
window.app.aModule.a();
} else {
//the public still hasnt been set, kick back an error
window.console.log('an error occurred');
}
};
}(jQuery));
因此,这遵循匿名函数作用域模式和 JSLints 没有问题。
这样更有意义? 没有理由将句柄传递给全局,仍然不确定为什么undefined
参数,但除此之外,这就是您链接的模式建议做的事情。
不过,我最大的怀疑是,你希望publicVarValue
是公开的。 我不确定为什么 - 如果你真的不需要公开它,那么上面是使用Dalgleish描述的模式的代码。
近?
顺便说一句,当达格利什建议时,我不明白...
您可以在jQuery本身中看到的一种技术是引用执行匿名函数时未定义的额外参数,实际上是为"undefined"创建一个别名:
检查此代码:
function wacky(p1, undefined)
{
if (p1 === undefined)
window.alert('undef');
else
window.alert('not undef');
}
var var1;
// var1 is undefined. It works.
wacky(var1); // undef
// Now you have a truthy value saying it's undefined.
// Wack.
wacky("spam", "spam"); // undef?
// Just to round out possibilities -- now
// truthy is not truthy. I'm not even sure
// what we're checking any more. ;^)
wacky("spam", "not spam"); // not undef?
我看不出有任何理由因为被这样覆盖而打开undefined
。 我理解动机,我认为 - 如果你总是传递正确数量的参数,那么undefined
将没有关联的参数,并且在某种程度上,将始终设置为undefined
。 我想,你会更有防御性地说...
function wacky(p1)
{
var spam;
undefined = spam;
//...
}
或者,正如这个答案所暗示的那样,使用void 0
来获得干净的undefined
。
但这可能绝对是多余的。 当我现在尝试将undefined
全局范围设置为Chrome中的某些内容时,它不会让我失望。 检查这个小提琴(很多警报;对不起)。
所以我买这家伙卖的东西。 不要执行undefined
参数技巧。 如果你不小心用一个额外的参数调用了一个函数,你就进入了一个痛苦的世界,Smokey。
- Node.js v6.2.0类扩展不是函数错误
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 试图在引导模式内动态生成图表,得到offsetWidth错误
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 我如何修复包含在captcha的addthis中的错误
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 节点是否需要模块传递带有方括号的arg?这是个错误吗
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- CKFinder 3为所选文件返回错误的URL
- 同位素库错误:未捕获错误无布局模式包装生产线8
- 铬:“;未捕获的语法错误:意外的标记:"
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- 相位器状态未捕获参考错误
- /undefined在我的404错误日志中多次出现
- Javascript未捕获语法错误意外的标识符错误
- javascript:如何在antlr生成的Lexer中进行错误处理
- js 对象的属性分配上的 lint 错误
- jQuery Lint的错误并不是描述性的
- Grunt lint错误使用$