Question

In the jquery.easing plugin there are many methods like this one:

easeInOutQuint: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
    return c/2*((t-=2)*t*t*t*t + 2) + b;
}

(t/=d/2) is severely pissing off jshint!

Linting assets/js/_main.js ...ERROR
[L119:C13] E030: Expected an identifier and instead saw '='.
    if ( (t/=d/2) < 1) {
[L119:C14] E020: Expected ')' to match '(' from line 119 and instead saw 'd'.
[L119:C19] W116: Expected '{' and instead saw '<'.
[L119:C19] E030: Expected an identifier and instead saw '<'.
[L119:C19] W030: Expected an assignment or function call and instead saw an expression.
[L119:C20] W033: Missing semicolon.
[L119:C21] W030: Expected an assignment or function call and instead saw an expression.
[L119:C22] W033: Missing semicolon.
[L119:C22] E030: Expected an identifier and instead saw ')'.
[L119:C22] W030: Expected an assignment or function call and instead saw an expression.
[L119:C23] W033: Missing semicolon.

(Removed repeated JS Lint output lines for brevity)

What is (t/=d/2) doing here?

I'd like to fix it (vs telling grunt to ignore it, which I've done for now) but I don't understand what it's doing. Regular expression of some sort? Note that t and d are passed in as arguments. Arithmetic shorthand? Both?

EDIT

Thanks for the blazing fast great answers. Changing the line to if ( (t = t / (d / 2)) < 1) got jshint to stop fussing. Will add answer with why jshint/jslint chose to throw this error. TL;DR: Because of exactly what happened to me: "Is this arithmetic or regex?"

Was it helpful?

Solution

Well jsHint is mistaken here. That's

t /= d / 2

which means

t = t / (d / 2)

Note that the parentheses are important in the alternative version, because normally the / operator binds left-to-right. The /= operator is of lower precedence than /.

Anyway the value of the whole thing will be the resulting (updated) value of "t".

Now, in a larger sense, jsHint might be right to complain about the obscurity of that, but that's a matter of style. The operator-assignment operators are a fairly old tradition, dating back at least to C.

OTHER TIPS

It's saying t = (t / (d / 2)); - the two statements are equivalent.

The /= operator is the unary version of the operator to divide by the right side operand and assign the result to the variable on the left.

It is shorthand.

$a /= $b; is the same as $a = $a / $b;

(t/=d/2) is the same as t = t / (d / 2)

Why I was getting the error in the first place:

This error is raised to highlight a potentially confusing piece of code. Your code will run fine if you do not fix this error, but it may be confusing to others, especially at first glance to someone quickly searching through your script.

The / character is ambiguous in JavaScript. It can either signify the start or end of a regular expression literal, as it does in the example above, or it can be interpreted as the division operator. Like most of the arithmetic operators, the division operator can be combined with the assignment operator to produce a shorthand:

From: http://jslinterrors.com/a-regular-expression-literal-can-be-confused-with/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top