Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bmeck
Last active March 29, 2018 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmeck/baa8f21cb0b33d4c0db3aad05a38473a to your computer and use it in GitHub Desktop.
Save bmeck/baa8f21cb0b33d4c0db3aad05a38473a to your computer and use it in GitHub Desktop.
some simple examples of out of class private. It can be used only lexically and cannot be stored into a variable. EVERYTHING HERE IS BIKESHED
// allocate a private field key
private #constructedHere;

export let create = () => {
  return {
    // cannot be used in computed properties
    // #constructedHere is not a variable with a reified value
    #constructedHere: true
  };
}
// entry
private #foo;
// export ... for is only for granting privilege
// very bikesheddy, this next line is 
export {#foo} for './friend';
// friend
// reuses regular import syntax
import {#foo as #bar} from './entry';
// untrusted
// fails during linking, entry has not granted privilege for this module to import #foo
import {#foo} from './entry';
private #data;
export function attach(o, v) {
  o.#data = v;
}
export function unwrap(o) {
  // throws if it does not exist
  let ret = o.#data;
  delete o.#data;
  return ret;
}
private #id;
let currentId = 1;

// attach a private id to an object and return the id
// will return the same id every time
export let getId = (o) => {
  // check if o has #id
  // bikesheddy, unlikely to reuse `in`
  if (#id in o !== true) {
    o.#id = currentId++;
  }
  return o.#id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment