Simplest/Cleanest way to implement singleton in JavaScript?

后端 未结 30 1061
名媛妹妹
名媛妹妹 2020-11-22 05:17

What is the simplest/cleanest way to implement singleton pattern in JavaScript?

30条回答
  •  一向
    一向 (楼主)
    2020-11-22 05:43

    function Unicode()
      {
      var i = 0, unicode = {}, zero_padding = "0000", max = 9999;
      //Loop through code points
      while (i < max) {
        //Convert decimal to hex value, find the character, then pad zeroes to the codepoint
        unicode[String.fromCharCode(parseInt(i, 16))] = ("u" + zero_padding + i).substr(-4);
        i = i + 1;
        }
    
      //Replace this function with the resulting lookup table
      Unicode = unicode;
      }
    
    //Usage
    Unicode();
    //Lookup
    Unicode["%"]; //returns 0025
    

提交回复
热议问题