Static variables in JavaScript

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

How can I create static variables in Javascript?

30条回答
  •  别那么骄傲
    2020-11-22 02:29

    you can use arguments.callee to store "static" variables (this is useful in anonymous function too):

    function () {
      arguments.callee.myStaticVar = arguments.callee.myStaticVar || 1;
      arguments.callee.myStaticVar++;
      alert(arguments.callee.myStaticVar);
    }
    

提交回复
热议问题