jridgewell / proposal-private-symbols Goto Github PK
View Code? Open in Web Editor NEWThis project forked from zenparsing/proposal-private-symbols
A proposal for private symbols in JavaScript
This project forked from zenparsing/proposal-private-symbols
A proposal for private symbols in JavaScript
Walking up the prototype chain on access to private is something we discussed the last time we looked into "static private" solutions. See the section of that repository for a summary of why we didn't select that option. @ljharb puts it succinctly in #43 (comment) :
it must be possible (and the common, default behavior) that any code whatsoever outside the
class
declaration, including superclasses or subclasses, can not observe anything about private fields; including their names, their values, their absence, or their mere existence.
@littledan
- tc39/proposal-class-fields#183 (comment)
To clarify further, if the word "private" is associated with it, I would expect that statement to apply - thus "private symbols walk the prototype chain" imo is a contradictory sentence.
@ljharb
- tc39/proposal-class-fields#183 (comment)
These are both arguing that private symbols are not actually private because they fire observable getPrototypeOf
traps (assuming a proxy is somewhere in the prototype chain, or the proxy is the base object).
const obj = { #x: 1 };
const p = new Proxy(obj, {
getPrototypeOf(target) {
console.log('observed');
}
});
const sub = Object.create(p);
// observable prototype lookup
p.#x
// observable prototype lookup
sub.#x
If this is a necessary requirement for privacy, why not just skip the getPrototypeOf
trap during prototype lookup? The same as get
/set
handler tunnel directly to the target, we can define the private symbol lookup algorithm to tunnel directly when traversing the prototype.
Originally I posted it in tc39/proposal-class-fields#206, but it seems that class-fields
were improper place for such proposals.
@jridgewell, I've checked your presentation and it seems that following proposal could be used to adjust your Symbol.private
approach. What do you think about that?
Use public
, private
(protected
as follow-up) as declaration keyword, like var
/let
/const
.
let x; // variable declaration
public #x; // create `Symbol()` and store it in lexically scoped constant `#x`
private #y; // create `Symbol.private()` and store it in lexically scoped constant `#y`
Any assignment without receiver leads to early SyntaxError
public #x = 1; // SyntaxError
public #y = Symbol(); // SyntaxError
public [#z] = [Symbol()]; // SyntaxError
#k = 1; // SyntaxError
#l = Symbol(); // SyntaxError
[#m] = Symbol(); // SyntaxError
Proper assignment always has receiver, []
and has no keyword.
public #x;
obj[#x] = 1;
The true power comes with computed property syntax.
public #x;
existingObj[#y] = 1; // throws because #y isn't declared yet
const obj = {
[#x]: 1,
public [#y]: 1,
private [#z]: 1,
};
const otherObj = {
private [#x]: 1, // shadows `#x` from outer lexical scope
[#y]: 1, // throws because #y isn't declared yet in this scope
private [#z]: 1,
};
There is another idea to closure scope instead of lexical one - in this case
private [#x]
inotherObj
will throw, since redeclaration of#x
is restricted. Both ways make some use-cases easier and others - more complex, but provide same feature set, so it's a discussible question which one to prefer.
Work mostly the same way as for objects.
public #x;
existingObj[#y] = 1; // throws because #y isn't declared yet
class SomeClass {
[#x] = 1;
public [#y] = 1;
private [#z] = 1;
};
class SomeOtherClass = {
private [#x] = 1; // shadowed `#x` not shared with `SomeClass`
public [#y] = 1; // another `#y` not shared with `SomeClass`
private [#z] = 1; // another `#z` not shared with `SomeClass`
};
#
stands for Symbol
. Any variable starting with this sign is ALWAYS Symbol
. So code like this private #x
should be read as private symbol x
.
I used already reserved keywords, since we are safe to use them + they are good fit for such mental model. But, obviously, we could select some others, for example:
let x; // variable declaration
sym #x; // create `Symbol()` and store it in closure scoped constant `#x`
psym #y; // create `Symbol.private()` and store it in closure scoped constant `#y`
// or even
#sym #y; // create `Symbol.private()` and store it in closure scoped constant `#y`
Symbol.protected
/Symbol.friend
/Symbol.<whatever>
and <whatever> #x
declaration syntax;<whatever> #x for 'key'
as shorthand for const x = Symbol.for('key')
;A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.