

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Classes
incomplete
2: Private Class Members
incomplete
3: TypeScript Public and Private
incomplete
4: Protected Data Members
incomplete
5: Abstract Classes and Methods
incomplete
6: Classes Implement Interfaces
incomplete
7: Classes vs. Interfaces and Types
incomplete
8: The 'this' Type
incomplete
9: Parameter Properties
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
JavaScript's # private fields didn't come until ES2022, but TypeScript developers had wanted public/private/protected access modifiers for a long time, so TypeScript added support for private and protected before then. So a lot of older TypeScript code uses the keyword syntax.
To create private members the TypeScript-only way, you use the private keyword:
class SecretAgent {
// private field using the private keyword
private id: string;
constructor(id: string) {
this.id = id;
}
// a public method
getCodeName(): string {
const idToCodeNameMap: { [key: string]: string } = {
"007": "James Bond",
"006": "Alec Trevelyan",
// Add more mappings as needed
};
return idToCodeNameMap[this.id] || "Unknown Agent";
}
}
const bond = new SecretAgent("007");
console.log(bond.getCodeName()); // "James Bond"
// Property 'id' is private and only accessible within class 'SecretAgent'
console.log(bond.id); // This will cause a compilation error
The only reason TypeScript-specific syntax exists is because JavaScript didn't have the # syntax until ES2022. I recommend using the JavaScript # syntax because it's the JavaScript native way to do it. Remember, TypeScript is basically just tools for writing type-safe JavaScript, so conforming to JavaScript standards is the long-term play.
I only recommend using the TypeScript specific syntax if you need to target an older version of JavaScript that doesn't support the # syntax yet.