Stann
Stann

Reputation: 13948

not understanding this javascript equation

Em. I'm looking at the easing equation here:

var easing = function( t, b, c, d ) {
   return c * ( t /= d ) * t * t * t + b;
}

So presumably one can write it like this:

var easing = function( t, b, c, d ) {
   return c * ( t = (t/d) ) * t * t * t + b;
}

or like this ? mmm.. not sure about this one:

var easing = function( t, b, c, d ) {
   return c * t = c * (t/d) * t * t * t + b;
}

How exactly is this equation being parsed by javascript, I mean, we get:

return number = number;

wtf? How is this being handled.

Upvotes: 0

Views: 97

Answers (2)

chess007
chess007

Reputation: 924

When an assignment expression appears in a larger expression, it evaluates to the value of the right-side operand. So in this case, the sub-expression ( t /= d ) takes on the value ( t / d ) in the overall expression. Also, since ( t /= d ) is the left-most sub-expression in parentheses, it is evaluated first (due to the standard PEMDAS order of operations), so t takes on the value ( t / d ) for the rest of the expression.

Upvotes: 1

Valera Kolupaev
Valera Kolupaev

Reputation: 2315

Assuming that expression are evaluated from left to right, the expression can be simplified by following steps:

1)

var easing = function( t, b, c, d ) {
   return c * ( t = (t/d) ) * t * t * t + b;
}

2)

var easing = function( t, b, c, d ) {
   t = t/d;
   return c * t * t * t * t + b;
}

3)

var easing = function( t, b, c, d ) {
   return c * Math.pow(t/d, 4) + b;
}

JsFiddle for the code: http://jsfiddle.net/caGWz/

Upvotes: 3

Related Questions