2

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

Will
  • 4,075
  • 1
  • 17
  • 28

4 Answers4

7

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.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Are you saying that `t / d / 2` is different from `t / (d / 2)`? :) – Ja͢ck Mar 12 '14 at 00:08
  • 1
    `1 / 3 / 2` = 0.1667, `1 / (3 / 2)` = 0.6667 So...yes. They are different. – nzifnab Mar 12 '14 at 00:09
  • 1
    Basic order of operations in mathematics. This is standard stuff that you should know by heart before even touching programming. Mainly because not all programming languages respect it, hence manually placing parentheses to explicitly group sections. – Derek Mar 12 '14 at 00:11
  • @Jack yep - think about it in normal algebraic terms. Division goes left to right in the absence of parens, and `(t / d) / 2` is different from `t / (d / 2)`. If you think about the division operations as multiplication by inverses, it becomes clearer: `t / d / 2` is `t * (1 / d) * (1 / 2)`, while `t / (d / 2)` is `t * (2 * 1 / d)` – Pointy Mar 12 '14 at 00:13
  • I know where my stupidity came from; I compared it to `x / y / z` vs `x / z / y` which is something else entirely ;-) time for coffee. – Ja͢ck Mar 12 '14 at 00:21
  • Ha ha my comment is backwards :) I've taken a lot of cold medicine today so a lot of stuff is backwards. – Pointy Mar 12 '14 at 00:24
1

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.

Derek
  • 4,575
  • 3
  • 22
  • 36
1

It is shorthand.

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

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

matcartmill
  • 1,573
  • 2
  • 19
  • 38
0

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/

Community
  • 1
  • 1
Will
  • 4,075
  • 1
  • 17
  • 28