letvalidator={set:function(obj,prop,value){if(prop==='age'){if(!Number.isInteger(value)){thrownewTypeError('The age is not an integer');}if(value>200){thrownewRangeError('The age seems invalid');}}// The default behavior to store the valueobj[prop]=value;// Indicate successreturntrue;}};constperson=newProxy({},validator);person.age=100;console.log(person.age);// 100person.age='young';// Throws an exceptionperson.age=300;// Throws an exception
All things software & product, honestly - FrontEnd, BackEnd, DevOps, ML - as long as we're solving massive problems.
My code is used by millions of users globally - Potatoes make me happy
for validations, do explore object proxies.
developer.mozilla.org/en-US/docs/W...
Proxies are not talked about enough!
Thanks for this!