Pat*_*rts 7 javascript standards jslint
我在JavaScript中有以下函数,当它通过JSLint运行时,它对我大吼大叫,就像我所说的那样.
function getPos(event, element) {
var x = event.clientX,
y = event.clientY,
currentElement = element;
do {
x -= currentElement.offsetLeft - currentElement.scrollLeft;
y -= currentElement.offsetTop - currentElement.scrollTop;
} while ((currentElement = currentElement.offsetParent));
return {
x: x,
y: y
};
}
Run Code Online (Sandbox Code Playgroud)
Specifically about the inline assignment expression in the while loop. I figured the double parentheses was the standard way of saying, "I'm expecting the returned value from the assignment expression to be type-casted to a Boolean for the conditional." JSLint seems to disagree, even when I enable "assignment expressions." Then I tried adding a !! in front, and JSLint complains that it's "confusing usage." So my question is, what's the right way of formatting this?
编辑:通过"this",我的意思是内联赋值表达式.我的问题的目的是澄清该特定线路的可接受标准是什么,如果一个人真的想要使用它,虽然我确实认为thefourtheye的答案是最正确的编写函数的方法,但它不是答案我问的问题.
根据这篇文章,对于 JSHint、ESHint 和 JSLint(2013 年 7 月之前),您可以将其强制转换为如下条件:
do {
// ...
} while ((currentElement = currentElement.offsetParent) !== null);
Run Code Online (Sandbox Code Playgroud)
对于 JSLint 的最新版本,您运气不好,如果您看重全绿色,则需要将您的作业分开。
| 归档时间: |
|
| 查看次数: |
98 次 |
| 最近记录: |