JavaScript - run once without booleans

后端 未结 9 1177
一个人的身影
一个人的身影 2021-01-30 17:42

Is there a way to run a piece of JavaScript code only ONCE, without using boolean flag variables to remember whether it has already been ran or not?

Spe

9条回答
  •  醉话见心
    2021-01-30 17:51

    In addition, the nature of what happens in the "/* do stuff here */" may leave something around that, when present, must mean that the function has run e.g.

    var counter = null;
    
    function initCounter() {
      if (counter === null) {
        counter = 0;
      }
    }
    

提交回复
热议问题