-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
private fields are not private #564
Comments
The simple fix won't work: ``` JavaScript`` var x = new Test(50); console.log(x.f()); // 100
|
I've just tested a couple of (non-)solutions, read that thread, found most of them there and got demotivated. Sorry, guys. There's no way to make private variables private without messing the generated JS a lot, while it must be human-readable by language design. |
What about using Object.defineProperty() to define the private field with enumerable = false, so that the TS private fields are not "discoverable" to Javascript code? Yes, they'd still be accessible if the client code knew what property name to ask for (as the TS class itself would), but I think this would address the 90% case for keeping private fields out of circulation. |
We don't want to have runtime behavior change meaningfully between ES3 and ES5 compilation targets (Object.defineProperty is not available in ES3). |
I don't believe that using The "discoverable" factor is already imposed by the IDE (VS for instance), which doesn't list private fields and methods in completion lists. The generated code when using |
@NoelAbrahams The IDE does show the private fields when debugging JS code that uses the TS productions. It just seems to me to be a really bad disconnect that the TS source declares X to be private when in fact it is openly visible and modifiable to the entire JS world. The whole point of access control is to hide implementation details and protect internal state from outside manipulation. TS "private" does neither. |
Many people would say the whole point of access control is to provide for cleaner layers of abstraction and reduce accidental use of other classes's implementation details. Enforcing 'private' as a compile-time construct accomplishes that goal. It's basically impossible to write a JavaScript program that is resilient to malicious or intentional attempts to manipulate its behavior from within the same execution environment. Trying to solve that problem with 'private' is a non-starter. |
This philosophy is problematic mainly for the for ... in scenario. To get around it, I'm using the underscore prefix convention for all private fields, and then checking for property.startsWith("_") in the for ... in loop. |
I can't understand why @RyanCavanaugh closed this issue !? |
Me neither, because I didn't? |
@acheshkov Let's imagine that TS compiled to assembly. Would |
Sorry guys, i lost the main topic of this issue. My concern is not about encapsulation with private keyword. I just wanted to give my vote to the idea of using Object.defineProperty(..{enumerable: false} ...) for private properties. @RyanCavanaugh the UX of github are confused looks like you closed it :) |
@acheshkov |
@polkovnikov-ph
|
Consider the following code
Typescript generates the following JS
Obviously, one could run something like
new Test().x
in a JS library, andprivate
invariant would fail. I think it's not a kind of behaviour one would expect fromprivate
, at least inexport
ed classes. The fix is quite simple:The text was updated successfully, but these errors were encountered: