const GokuldhamSociety = (function() {
// Private variables
let residents = [];
let societyFunds = 100000;
const secretCode = "TMKOC2024";
// Private functions
function validateResident(name) {
return name && name.length > 0;
}
// Public API (returned object)
return {
addResident: function(name) {
if (validateResident(name)) {
residents.push(name);
return `${name} society mein add hua!`;
}
return "Resident ka naam galat hai!";
},
getResidents: function() {
return [...residents]; // Original nahi, copy return karta hai
},
collectFunds: function(amount) {
societyFunds += amount;
return `âš${amount} collect kiye. Total funds: âš${societyFunds}`;
}
};
})(); // IIFE - Turant Execute Hone Wala Function
console.log(GokuldhamSociety.addResident("Jethalal"));
console.log(GokuldhamSociety.addResident("Babita"));
console.log(GokuldhamSociety.getResidents());
console.log(GokuldhamSociety.collectFunds(5000));
// â Private data accessible nahi
console.log(GokuldhamSociety.residents); // undefined
console.log(GokuldhamSociety.societyFunds); // undefined
console.log(GokuldhamSociety.secretCode); // undefined