Static variables in JavaScript

后端 未结 30 2270
别那么骄傲
别那么骄傲 2020-11-22 01:55

How can I create static variables in Javascript?

30条回答
  •  鱼传尺愫
    2020-11-22 02:32

    In JavaScript variables are static by default. Example:

    var x = 0;
    
    function draw() {
        alert(x); //
        x+=1;
    }
    
    setInterval(draw, 1000);
    

    The value of x is incremented by 1 every 1000 milliseconds
    It will print 1,2,3 so forth

提交回复
热议问题