Having an object that is made from a class definition:
class Something {
get prop() {
...
}
}
const s = new Something();
How to detect in code that s.prop
is read-only property and cannot be set (and without trying to set it)? What I’ve already tried is to use Reflect.getOwnPropertyDescriptor but it returns undefined
in this case.
2
Answers
Stolen and adapted from https://stackoverflow.com/a/70250484/13111630, but you can use these functions to find all the setters and getters for your class.
In your example, you can run
listSetters
on yourSomething
instance to see that there’s aprop
getter but noprop
setter.Try this: