Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the standard format for assignment expressions?

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.

like image 926
Patrick Roberts Avatar asked Jan 10 '15 06:01

Patrick Roberts


People also ask

What is an assignment expression?

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.

What is assignment expression in C?

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.

What is the typical evaluation order for an assignment expression?

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.


2 Answers

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.

like image 163
Ja͢ck Avatar answered Sep 29 '22 22:09

Ja͢ck


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.

like image 35
thefourtheye Avatar answered Sep 29 '22 22:09

thefourtheye