I have the class:
class Course {
constructor(name) {
this._name = name;
}
getName() {
return this._name;
}
}
I want to create a proxy around class Course, that will return only non-private fields and methods:
const handlers = {
get: (target, prop, reciever) => {
if (prop in reciever && prop[0] !== '_' ) {
if (typeof reciever[prop] === 'function') {
return reciever[prop].apply(reciever);
} else {
return reciever[prop];
}
} else {
throw new Error('problem');
}
}
}
const protect = (obj) => {
return new Proxy(obj, handlers);
}
But when i call object method:
const protectedCourse = protect(new Course('Test'));
console.log(protectedCourse.getName()); // The expected result is - "Test"
I got the error:
if (prop in reciever && prop[0] !== '_' ) {
^
RangeError: Maximum call stack size exceeded
at Object.get (file:///usr/src/app/protect.js:37:5)
at Object.get (file:///usr/src/app/protect.js:38:26)
at Object.get (file:///usr/src/app/protect.js:38:26)
at Object.get (file:///usr/src/app/protect.js:38:26)
at Object.get (file:///usr/src/app/protect.js:38:26)
at Object.get (file:///usr/src/app/protect.js:38:26)
at Object.get (file:///usr/src/app/protect.js:38:26)
at Object.get (file:///usr/src/app/protect.js:38:26)
at Object.get (file:///usr/src/app/protect.js:38:26)
at Object.get (file:///usr/src/app/protect.js:38:26)
How to call object method without infinity recursion in the handler of Proxy?