Last active
February 12, 2019 17:04
-
-
Save littledan/d3030534cf96075d47228955828f932e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can it be accessed in different classes?