You can use the method .bind()to bind window.consoleto your custom logging method, and then return the function so that the code runs in the original scope when it is called.
log:
class LoggerService {
public log = console.log.bind(window.console);
}
// ...or annotated:
class LoggerService {
public log: (message) => void = console.log.bind(window.console);
}
, :
class LoggerService {
public log = clientSideLogging ? console.log.bind(window.console) : () => {};
}
TypeScript.
, log, getter, console.log, window.console.
class LoggerService {
public get log (): Function {
return console.log.bind(window.console);
}
}
, , console.log , , .
, :
class LoggerService {
public get log (): Function {
const log = console.log.bind(window.console);
return clientSideLogging ? log : () => {};
}
}
TypeScript.