const obj = {
get foo() {
return Math.random() > 0.5 ? "foo" : "bar";
},
};
obj.__lookupGetter__("foo");
// [Function: get foo]Object.prototype.__lookupGetter__() / notebook
Object.prototype.__lookupGetter__()
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const obj = {
get foo() {
return Math.random() > 0.5 ? "foo" : "bar";
},
};
Object.getOwnPropertyDescriptor(obj, "foo").get;
// [Function: get foo]const obj2 = {
__proto__: {
get foo() {
return Math.random() > 0.5 ? "foo" : "bar";
},
},
};
function findGetter(obj, prop) {
while (obj) {
const desc = Object.getOwnPropertyDescriptor(obj, prop);
if (desc) {
return desc.get;
}
obj = Object.getPrototypeOf(obj);
}
}
console.log(findGetter(obj2, "foo")); // [Function: get foo]