Search code examples
javascriptjqueryeasing

What does (t/=d/2) mean in this code?


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?"


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.