Hacker News new | ask | show | jobs
by colejohnson66 1608 days ago
> I honestly don't care what this transpiles down to, the point for us at least is not to make things "truly private" (good luck with that in JavaScript). It's simply to compiler-enforce rules.

It’s not just JavaScript. With reflection in C# and Java, you can mess around with private variables from outside the classes. For Java, this can have some pretty interesting results, such as 2+2 being equal to 5.[0] The whole point of compiler level annotations is to keep good programmers honest.

If some devious JavaScript developer wants to ruin your library, that's their fault.

[0]: https://codegolf.stackexchange.com/a/28818/13944

3 comments

I mean, I don't know if you can do this with modern c++, but I would occasionally do this when I was being aggressive in my c++ tweaks:

    #define private public
    #import <something.h>
then you can interact with your class private fields all you want.
Agreed, with reflection you can get around that also, but in JS it's even less of a "real thing" at the moment and I don't see that changing until the far future where we can drop some of this legacy cruft.
> With reflection, you can mess around with private variables from outside the classes.

Worth mentioning that you can disallow reflection via the security manager, at least in earlier versions of the JVM.