admin管理员组

文章数量:1296854

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);

});

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);

});
Share edited Jul 15, 2015 at 21:21 JasonMArcher 15k22 gold badges59 silver badges53 bronze badges asked Jan 9, 2010 at 20:34 Bob DylanBob Dylan 4,46311 gold badges43 silver badges58 bronze badges 4
  • 2 This is obviously copied from stackoverflow./questions/1642028/… – Dave O. Commented Jan 9, 2010 at 21:05
  • This question is about Javascript. The one you reference is about C/C++. It's true they are similar, and possibly even copied, but I think this is a fair enough variant since it asks about a different language. – Rob Levine Commented Jan 9, 2010 at 23:07
  • @Rob Levine: Great!, I'll post a similar question for every programming language that supports both the post decrement operator and the greater than operator :-P (and for every language that supports pre decrement and less than operators xD) – Dave O. Commented Jan 10, 2010 at 23:08
  • 1 stackoverflow./q/1642028/194544 – beryllium Commented Feb 20, 2012 at 13:14
Add a ment  | 

4 Answers 4

Reset to default 13

i-->0 is the same as i-- > 0, so the parison 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.

本文标签: What does the igt opeator do in JavaScriptStack Overflow