Proxies in JavaScript
Validate marks in JavaScript with Proxy get/set traps and Reflect. Reject integers outside 0–20; writes to the target object directly bypass the traps.
Run this example once in a fresh browser console. The console shows the printed results.
1const student = { name: 'Asha', marks: 18 };2const record = new Proxy(student, {3 get(target, property, receiver) {4 if (!(property in target)) return 'Not recorded';5 return Reflect.get(target, property, receiver);6 },7 set(target, property, value, receiver) {8 if (property === 'marks' && (!Number.isInteger(value) || value < 0 || value > 20)) {9 throw new RangeError('Marks must be an integer from 0 to 20');10 }11 return Reflect.set(target, property, value, receiver);12 }13});14console.log(record.name);15record.marks = 20;16console.log('Marks:', record.marks);17console.log(record.section);18try { record.marks = 25; } catch (error) { console.log(error.message); }19console.log('Stored marks:', student.marks);
Sample Output
Marks: 20
The set trap accepted the integer 20 and forwarded it to the target.
Not recorded
The get trap supplied the chosen fallback for a missing section.
Stored marks: 20
The rejected 25 assignment threw before modifying the target.
In this one
- Wrap an object with get and set traps
- Validate a property before writing
- Forward normal work with Reflect
- Recognize direct-target bypasses
How it works
- 1
Create a student object with name and marks.
- 2
Wrap it in a Proxy with reading and writing handlers.
- 3
Read name and set marks to the allowed value 20.
- 4
Read missing section to see the fallback.
- 5
Attempt 25, catch the range error, and confirm stored marks remain 20.
Edge cases
Invalid marks
Negative, fractional, and above-20 values must be rejected.
The set trap checks integer status and both bounds before forwarding the write.
Direct target access
student.marks = 99 would not use record at all.
Keep the target controlled if you want all local writes checked. Still validate untrusted data at a real system boundary.
What's going on
A proxy is an object that stands in front of another object and can customize some operations on it. A trap is a handler function that runs for one of those operations. This marks-record example uses a get trap for reading and a set trap for writing.
The get trap returns Not recorded for a missing property. Existing properties are read with Reflect.get, which performs the normal property-reading operation. The set trap checks marks before forwarding the write with Reflect.set. Values outside the integer range from 0 to 20 throw an error, so 25 never replaces the accepted score of 20.
The proxy and student are not independent copies. Accepted writes through record change student. Code that writes directly to student bypasses these traps, so the proxy is not a security boundary or a substitute for server validation. Returning a fallback for every missing property is a teaching choice here, not always a good general-object design. Keep such behavior predictable for the objects you actually control.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which trap runs for record.marks = 20?
Pick an answer to check it.
Question 2
What remains after the rejected value 25?
Pick an answer to check it.
Questions people ask
Does new Proxy copy the target?
No. It creates a wrapper. Accepted operations still act on the underlying target.
Why return Reflect.set?
A set trap must report whether the write succeeded. Reflect.set provides the ordinary write result.
Can I validate data securely with this alone?
No. Browser code can be changed, and direct access to the target bypasses the proxy. Use trusted validation where data is accepted.