ztech ztech - 3 months ago 15
Javascript Question

Closure compiler added '--' to my Javascript function. What does it mean?

I have a simple function that I ran through Google's Closure Compiler Service:

var fisherYatesShuffle = function(array) {
var currentIndex = array.length;
var temporaryValue;
var randomIndex;

// While there remain elements to shuffle...
while (0 !== currentIndex) {

// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;

// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}

return array;
};


It gave me back the following (which I've pretty-printed):

var fisherYatesShuffle = function(a) {
for (var b = a.length, d, c; 0 !== b;) c = Math.floor(Math.random() * b), --
b, d = a[b], a[b] = a[c], a[c] = d;
return a
};


What is this 'dash dash' rass at the end of line two? Why have I never seen it?

Answer

It's -- b which is --b pre-decrement operator.

Same as

b = b - 1;

It is split on the next line, because there are already 80 characters(max) on that line.

Comments