Access Modifiers (public, private, protected) की जानकारी
In this page:
class ClassName {
public publicProp: type;
private privateProp: type;
protected protectedProp: type;
}
Public Members
Public members कहीं से भी access किए जा सकते हैं जहां object खुद accessible है, और public TypeScript classes में default visibility है जब कोई modifier बिल्कुल नहीं लिखा जाता।
उदाहरण: Public Members
class Person {
public name: string = "Guest"; // public is also the default
}
const p = new Person();
console.log(p.name);
Private Members
Private members सिर्फ उस class के अंदर access किए जा सकते हैं जो उन्हें declare करती है, जो implementation details जैसे internal caches या helper state छिपाने के लिए उपयोगी है जिन्हें outside code को कभी सीधे touch नहीं करना चाहिए।
उदाहरण: Private Members
class BankAccount {
private balance: number = 0;
deposit(amount: number) {
this.balance += amount;
}
getBalance() {
return this.balance;
}
}
const acc = new BankAccount();
acc.deposit(100);
console.log(acc.getBalance());
Protected Members
Protected members declaring class के अंदर और उन classes के अंदर access किए जा सकते हैं जो इससे inherit करती हैं, एक middle ground strike करते हुए जो उन values के लिए उपयोगी है जिन पर एक subclass को build करने की ज़रूरत हो लेकिन outside callers को नहीं देखना चाहिए।
उदाहरण: Protected Members
class Animal {
protected sound: string = "...";
}
class Dog extends Animal {
bark() {
console.log(this.sound);
}
}
new Dog().bark();
Modifiers को Combine करना
एक class अपने members में अलग-अलग access modifiers mix कर सकती है, एक छोटा public API expose करते हुए जबकि supporting fields और helper methods को private या protected रखते हुए, जो encapsulation का essence है।
उदाहरण: Combining Modifiers
class User {
public name: string;
private password: string;
protected role: string;
constructor(name: string, password: string, role: string) {
this.name = name;
this.password = password;
this.role = role;
}
}
const u = new User("Sam", "secret", "admin");
console.log(u.name);
सही Modifier चुनना
उन values के लिए public उपयोग करें जो object के accessible interface का हिस्सा हैं, implementation details के लिए private जिन पर कोई और code depend नहीं करना चाहिए, और protected उस किसी भी चीज़ के लिए जिसे एक future subclass extend या override करने की उम्मीद है।
उदाहरण: Choosing the Right Modifier
class Order {
public id: number; // part of the public interface
private total: number; // implementation detail
protected status: string; // usable by subclasses
constructor(id: number, total: number, status: string) {
this.id = id;
this.total = total;
this.status = status;
}
}
console.log(new Order(1, 50, "pending").id);
- यह मानना कि
privateruntime पर data छिपाता है, जब यह सिर्फ compiler द्वारा check किया जाता है और value emitted JavaScript में अब भी visible है। - एक
protectedmember को class के बाहर से access करना, जैसेobj.sound, जो एक error है और सिर्फ class और इसकी subclasses में allowed है। - एक constructor parameter में
privateजोड़ना यह realize किए बिना कि यह एक property भी बनाता है, या यह उम्मीद करना कि दूसरी classes इसे directly पढ़ सकेंगी।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: