1

I have the following function that I use to generate string-based unique IDs in a JavaScript program

var NewUid= (function () {
    var _lastID = 0;
    return function() {
        return (_lastID++).toString(36); // *
    }
})();

It has worked fine up to now. It should do since it's so basic. However, it's just failed on me in Chrome, by continually throwing a TypeError on the line marked * . The console log states:

Uncaught TypeError: Number.prototype.toString is not generic

The _lastID variable was at about 200000 when it occurred, so nothing too ridiculous. I've not been able to reproduce it so far. Can anyone tell why this may have happened?

I'm using Chrome '29.0.1547.76 m' if that helps.

Barguast
  • 5,926
  • 9
  • 43
  • 73
  • This looks related: http://stackoverflow.com/questions/3261587/subclassing-javascript-arrays-typeerror-array-prototype-tostring-is-not-generi – Barmar Oct 10 '13 at 15:02
  • Shot in the dark... Does this do anything different: `return Number.prototype.toString.call((_lastID++), 36);` – lbstr Oct 10 '13 at 15:03
  • You sure lastID is never a decimal? – jbarnett Oct 10 '13 at 15:05
  • @jbarnett - Yes. _lastID isn't accessible outside of this function. It is only initialised to 0, and incremented thereafter as shown. – Barguast Oct 10 '13 at 15:09
  • @lbstr - I can't reproduce it, so I can't currently check. Oddly, I could run the same line of code in the 'Watch Expressions' section of the Chrome debugger with no problems. – Barguast Oct 10 '13 at 15:11
  • @Barmar - the error looks similar, but apart from that I think it's unrelated..? – Barguast Oct 10 '13 at 15:11
  • why have you used immediately invoked function, instead of normal declaration ? – Anand Oct 10 '13 at 15:13
  • See this JS fiddle, that I have used with simple declaration of class. I start at 200000. it works fine on chrome. http://jsfiddle.net/AnandVishnu/G6vPS/ – Anand Oct 10 '13 at 15:14
  • So does the above code - it has worked consistently up to now, with _lastID exceeding 1,000,000 and more. It just decided to fail on me one day! It may be a glitch, as I've been unable to get it to happen again. Just hoping someone might know why. – Barguast Oct 10 '13 at 15:25
  • @Anand his example also works fine in fiddle, so its nothing to do with iife: http://jsfiddle.net/lbstr/ApeRy/ – lbstr Oct 10 '13 at 15:26

0 Answers0