This project is read-only.

Private variables in classes

Topics: Language Specification
Jul 24, 2013 at 10:10 PM
Edited Jul 24, 2013 at 10:10 PM
Currently I see that TypeScript provides a syntax for private members on classes, but the generated javascript actually creates a public property.

As a workaround, could the developers generate a property that is declared using the var keyword withing the class definition?

If they don't want to do that, it would be really helpful if the compiler could at least permit private members declared using the "var" keyword within the class definition body.

Right now, in order to get private members that I can use in my class, I define my private member using the "var" keyword outside of the class definition and I wrap my code in a module, to avoid polluting the global namespace. Defining my private members outside of my class definition makes for very ugly code.

Is it possible that some kind of workaround for this could be implemented by version 1? It would provide actual private members on classes, and I wouldn't have to declare my private variables (using "var") outside of my class definition.

I'm very excited about this language, and I've really enjoyed using it in a javascript app for salesforce.
Jul 25, 2013 at 1:02 AM
This has been discussed before, and the issue is that the patterns for enforced private members in JavaScript have a lot of performance overhead. TypeScript is mainly targeting large applications, so compiling to code with performance overhead is not optimal.