So I took a look at the code that controls the counter on the SO advertising page. Then I saw the line where this occured i-->
. What does this do?
Here is the full code:
$(function(){
var visitors = 5373891;
var updateVisitors = function开发者_运维知识库()
{
visitors++;
var vs = visitors.toString(),
i = Math.floor(vs.length / 3),
l = vs.length % 3;
while (i-->0) if (!(l==0&&i==0)) // <-------- Here it is!!!
vs = vs.slice(0,i*3+l)
+ ','
+ vs.slice(i*3+l);
$('#devCount').text(vs);
setTimeout(updateVisitors, Math.random()*2000);
};
setTimeout(updateVisitors, Math.random()*2000);
});
i-->0
is the same as i-- > 0
, so the comparison expression if the evaluated value of i--
is greater than 0
.
it is not an operator. See this link:
What is the "-->" operator in C++?
var i = 10;
while (i-- > 0)
{
alert('i = ' + i);
}
Output:
i = 9
i = 8
i = 7
i = 6
i = 5
i = 4
i = 3
i = 2
i = 1
i = 0
Other answers have explained that it's two operators. I'll just add that in the example, it's unnecessary. If you're counting down from a positive integer to zero, you can miss out the greater-than-zero test and your code will be shorter and, I think, clearer:
var i = 10;
while (i--) {
// Do stuff;
}
Thought of the exact same thread that JCasso thought of. What is the "-->" operator in C++?
I think this code style stems from the early days of programming when terminals had limited display real estate.
精彩评论