Frameworks like Vue use Proxy for reactivity. You can too – update UI automatically when data changes.
Basic Reactive Object:
const data = {
count: 0,
name: 'John'
};
const reactive = new Proxy(data, {
set(target, property, value) {
target[property] = value;
// Update UI automatically
document.getElementById(property).textContent = value;
console.log(`${property} changed to ${value}`);
return true;
}
});
// Usage
reactive.count = 5; // UI updates automatically!
reactive.name = 'Jane'; // UI updates automatically!
Validation:
const validated = new Proxy({}, {
set(target, property, value) {
if (property === 'age' && (value < 0 || value > 150)) {
throw new Error('Invalid age');
}
target[property] = value;
return true;
}
});
validated.age = 25; // OK
validated.age = -5; // Error!
Build reactive systems without framework overhead!
