For example (from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set), I init an object like this:
var o = {
set current (str) {
this.log[this.log.length] = str;
},
log: []
}
How can I print the function body of o.current in console? If o.current is a plain method, it can be done by o.current.toString().. However, I have no idea about how to print the function body of a "getter" or "setter" method.
Does anyone have ideas about this?
You can use
__lookupSetter__for this. So simply callingo.__lookupSetter__('current').toString()should give you your desired output. SourceUpdate
This is already deprecated though. You should be using the standard
Object.getOwnPropertyDescriptorinstead. So callingObject.getOwnPropertyDescriptor(o, 'current').set.toString()will work for your use case. Source