Skip to content

Instantly share code, notes, and snippets.

@kimtrien
Created April 4, 2020 15:26
Show Gist options
  • Save kimtrien/b175b0ec86e110f0d115e88ed68cbc47 to your computer and use it in GitHub Desktop.
Save kimtrien/b175b0ec86e110f0d115e88ed68cbc47 to your computer and use it in GitHub Desktop.

Revisions

  1. @gordonbrander gordonbrander revised this gist Mar 28, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions ID.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Generate pseudo-private name string objects.
    // Generate unique IDs for use as pseudo-private/protected names.
    // Similar in concept to
    // <http://wiki.ecmascript.org/doku.php?id=strawman:names>.
    //
    @@ -12,9 +12,9 @@
    //
    // Use:
    //
    // var pid = ID();
    // var o = { 'public': 'foo' };
    // o[pid] = 'bar';
    // var privateName = ID();
    // var o = { 'public': 'foo' };
    // o[privateName] = 'bar';
    var ID = function () {
    // Math.random should be unique because of its seeding algorithm.
    // Convert it to base 36 (numbers + letters), and grab the first 9 characters
  2. @gordonbrander gordonbrander created this gist Mar 28, 2012.
    23 changes: 23 additions & 0 deletions ID.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    // Generate pseudo-private name string objects.
    // Similar in concept to
    // <http://wiki.ecmascript.org/doku.php?id=strawman:names>.
    //
    // The goals of this function are twofold:
    //
    // * Provide a way to generate a string guaranteed to be unique when compared
    // to other strings generated by this function.
    // * Make the string complex enough that it is highly unlikely to be
    // accidentally duplicated by hand (this is key if you're using `ID`
    // as a private/protected name on an object).
    //
    // Use:
    //
    // var pid = ID();
    // var o = { 'public': 'foo' };
    // o[pid] = 'bar';
    var ID = function () {
    // Math.random should be unique because of its seeding algorithm.
    // Convert it to base 36 (numbers + letters), and grab the first 9 characters
    // after the decimal.
    return '_' + Math.random().toString(36).substr(2, 9);
    };