Static variables in JavaScript

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

How can I create static variables in Javascript?

30条回答
  •  囚心锁ツ
    2020-11-22 02:44

    In JavaScript, there is no term or keyword static, but we can put such data directly into function object (like in any other object).

    function f() {
        f.count = ++f.count || 1 // f.count is undefined at first
        alert("Call No " + f.count)
    }
    
    f(); // Call No 1
    
    f(); // Call No 2
    

提交回复
热议问题