Skip to content

Instantly share code, notes, and snippets.

@littledan
Last active February 12, 2019 17:04
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 littledan/d3030534cf96075d47228955828f932e to your computer and use it in GitHub Desktop.
Save littledan/d3030534cf96075d47228955828f932e to your computer and use it in GitHub Desktop.
// In the following code, take `private` and `friend` to be extremely early strawperson names
private #x; // Declare a private name
let obj = { outer #x: 1 };
class MyClass {
outer #x;
constructor(x) { this.#x = x }
}
// #x can be accessed from functions in the lexical scope of the `private` declaration
function getX(obj) {
return obj.#x;
}
getX(new MyClass(2)); // 2
getX(obj); // 1
// Usable from destructuring
let { outer #x: a } = obj; // a = 1
// Can be used for friend methods as well
{
private #y;
class Z {
outer #y() {
alert('hi');
}
}
new Z().#y(); // alert hi
}
// Cannot be accessed outside block scope--early error
let y = {outer #y: 2}; // SyntaxError
@ByteEater-pl
Copy link

Can it be accessed in different classes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment