Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.