I have the following function in JavaScript and when running it through JSLint, it yelled at me, as I call it.
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
};
}
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?
EDIT: By "this", I meant specifically the inline assignment expression. The intent of my question was to clarify what an acceptable standard for that particular line was, if one really wanted to use that, and though I do agree that thefourtheye's answer is the most correct way to write the function, it is not the answer to the question I was asking.
Assignment expressions allow you to assign and return a value in the same expression. For example, if you want to assign to a variable and print its value, then you typically do something like this: >>> >>> walrus = False >>> print(walrus) False.
An assignment operation assigns the value of the right-hand operand to the storage location named by the left-hand operand. Therefore, the left-hand operand of an assignment operation must be a modifiable l-value. After the assignment, an assignment expression has the value of the left operand but is not an l-value.
The Usual Order ..., when evaluating the operands of an expression, assignment, or return statement, all function calls, method calls, and (channel) communication operations are evaluated in lexical left-to-right order.
According to this article, for JSHint, ESHint and JSLint (from before July 2013) you can force it into a conditional like this:
do {
// ...
} while ((currentElement = currentElement.offsetParent) !== null);
For more recent versions of JSLint you're out of luck and you need to separate out your assignment if you value the all green.
It is better not to have assignment expressions in conditional expressions. But, I believe that while
would suit this case better than do...while
. You could transform the code to this
function getPos(event, element) {
var x = event.clientX,
y = event.clientY,
currentElement = element;
while (currentElement) {
x -= currentElement.offsetLeft - currentElement.scrollLeft;
y -= currentElement.offsetTop - currentElement.scrollTop;
currentElement = element.offsetParent;
}
return {
x: x,
y: y
};
}
Now, there is no assignment involved in the conditional expression and I believe this looks clean.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With