JSLint类型混淆:带有jQuery .css()的函数和对象 [英] JSLint Type Confusion: function and object with jQuery .css()
问题描述
我很难让这些行使用JSLint进行验证.我假设(也许是错误地)它们实际上是有效的.这是相关代码:
I am having difficulty getting these lines to validate using JSLint. I'm assuming (perhaps incorrectly) that they are actually valid. Here is the relevant code:
var shadowBox = $('<div/>').appendTo($(document.body));
// ...
shadowBox.css({
left: (e.originalEvent.clientX + 10).toString() + 'px',
top: e.originalEvent.clientY.toString() + 'px'
});
我收到的验证错误是:
第492行第22个字符处的问题:类型混淆:函数和.css:对象.
Problem at line 492 character 22: Type confusion: function and .css: object.
shadowBox.css({
shadowBox.css({
和
第494行字符57处的问题:类型混淆:数字和'+':字符串.
Problem at line 494 character 57: Type confusion: number and '+': string.
顶部:e.originalEvent.clientY.toString()+'px'
top: e.originalEvent.clientY.toString() + 'px'
第一个让我更加烦恼,因为那是一种相当常见的jQuery语法. 第二个似乎是误报,因为我正在将string与string连接起来.
I'm troubled more by the first one as that is a rather common jQuery syntax. The second seems like a false positive since I am concatenating string with string.
谢谢
编辑
找出问题所在:
结果证明JSLint对同一文档中存在的以下两种语法模式不满意:
Turns out JSLint is not happy with these two syntax patterns existing in the same document:
var $var = $('<div/>', {css: {width: '15px'}}); // ONE
$var.css({width : '20px'}); // TWO
推荐答案
结果证明JSLint增加了其类型混淆"规则.
Turns out that JSLint has increased their "type confusion" rules.
我认为类型混淆纯粹是这样定义的:
Whereas I thought type confusion was defined purely as this:
var a = 1;
var b = a + 'string';
事实证明,这也被克罗克福德先生认为是类型混淆(我不太同意他的观点)
Turns out this is also considered type confusion by Mr. Crockford (I don't particularly agree with him)
function MyObject () {
this.top() {
return '15px';
}
}
var anonObject = {top: '15px'};
var myObject = new MyObject();
var testString = myObject.top();
或jQuery上下文中的一个简单得多的示例
or a much simpler example in the context of jQuery
var $b = $('<div/>').css({top: '15px'}),
a = $b.offset().top + 15;
我的印象是类型混淆仅指标识符会在范围内更改值.尽管克劳福德先生似乎在想:
I was under the impression that type confusion referred solely to identifiers changing value within scope. Though Mr. Crawford seems to think:
来自:道格拉斯·克罗克福德
From: Douglas Crockford
主题:Re:JSLint类型问题
Subject: Re: JSLint Type Problems
致:安东尼S."
日期:2011年6月23日,星期四,上午11:44
Date: Thursday, June 23, 2011, 11:44 AM
您似乎对什么是类型混淆感到困惑.如果你不这样做 想要看到警告,则不必.
You seem to be confused about what type confusion is. If you don't want to see the warnings, then you don't have to.
这篇关于JSLint类型混淆:带有jQuery .css()的函数和对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!