I have a project where I extract some data using APIs and load it into relevant spreadsheets using Google App Script. I want to log the performance (time) of each function that runs, so I thought I would write a decorator function that takes a function as a parameter and calculate it's run time and then logs it. And use this decorator in my app for each function.
But when I take a class function as a parameter it doesn't see the class object of the function, therefore I'm not able to access class object's properties within the function using this.
In order to demonstrate what I'm trying to achieve I write the draft codes below, is there a way to access class object's properties by using class function as a parameter?
function performanceLogger(func,...params){
var logger = new ProjectLogger("logs")
try{
var startTime = new Date().getTime();
var data = func(...params)
var endTime = new Date().getTime();
logger.logToSheet(`${func.name} function run without a issue Time: ${endTime-startTime} ms.`)
return data
} catch(e){
logger.logToSheet(`${func.name} function didn't run, there is an error.`,true)
}
}
class Debuggin_Class{
constructor(){
this.name = "debugger class"
}
debug_f(){
return `${this.name} is debugging`;
}
}
function app(){
let debug_c = new Debuggin_Class();
return performanceLogger(debug_c.debug_f)
}
error: "Cannot read property 'name' of undefined"