Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  189] [ 1]  / answers: 1 / hits: 114188  / 12 Years ago, wed, october 3, 2012, 12:00:00

I'm looking at implementation of private members in TypeScript, and I find it a little confusing. Intellisense doesn't allow to access private member, but in pure JavaScript, it's all there. This makes me think that TS doesn't implement private members correctly.
Any thoughts?



class Test{
private member: any = private member;
}
alert(new Test().member);

More From » typescript

 Answers
3

Just as with the type checking, the privacy of members are only enforced within the compiler.



A private property is implemented as a regular property, and code outside the class is not allowed to access it.



To make something truly private inside the class, it can't be a member of the class, it would be a local variable created inside a function scope inside the code that creates the object. That would mean that you can't access it like a member of the class, i.e. using the this keyword.


[#82770] Tuesday, October 2, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ariel

Total Points: 523
Total Questions: 111
Total Answers: 100

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
;