Jinsi ya Kutumia JS Proxy kwa Data Observation
Hatua kwa Hatua
1️⃣ Unda Object na Proxy
const user = { name: "Faustine", age: 28 };
// Handler ina method zinazoshughulikia operations
const handler = {
get(target, property) {
console.log(`Property "${property}" imepitwa`);
return target[property];
},
set(target, property, value) {
console.log(`Property "${property}" imebadilishwa kutoka "${target[property]}" kwenda "${value}"`);
target[property] = value;
return true;
},
deleteProperty(target, property) {
console.log(`Property "${property}" imefutwa`);
delete target[property];
return true;
}
};
// Unda Proxy
const proxyUser = new Proxy(user, handler);
2️⃣ Fuatilia Operations
// Kupata property
console.log(proxyUser.name); // Output: Property "name" imepitwa
// Kubadilisha property
proxyUser.age = 30; // Output: Property "age" imebadilishwa kutoka "28" kwenda "30"
// Kufuta property
delete proxyUser.name; // Output: Property "name" imefutwa
3️⃣ Faida za Kutumia JS Proxy
Reactive State: Fuatilia mabadiliko ya state kwa urahisi.
Validation: Angalia au reject values kabla ya ku-update object.
Logging & Debugging: Gundua usage ya object kwa console logs.
Security / Access Control: Zuia access kwa properties fulani.
🔗 Links Za Kujifunza Zaidi
🌐 Faulink Official Website: https://www.faulink.com/
📘 Jifunze Web Design & Programming (Tutorials / Mifumo): https://www.faulink.com/excel_mifumo.php
📲 Piga / WhatsApp kwa msaada wa haraka: https://wa.me/255693118509