I have a JavaScript function that utilizes both the `this` keyword and captures arguments:
var Watcher = function() {
var callbacks = [];
var currentValue = null;
this.watch = function (callback) {
callbacks.push(callback);
if (currentValue) {
callback.apply(null, currentValue);
}
};
this.notify = function() {
currentValue = arguments;
for (var i = 0; i < callbacks.length; i++) {
callbacks[i].apply(null, arguments);
}
};
}
I am looking to convert this into a TypeScript class. Here is what I have so far:
class Watcher {
private currentValue: any[] = null;
private callbacks: Function[] = [];
watch = (callback: Function) => {
this.callbacks.push(callback);
if (this.currentValue) {
callback.apply(null, this.currentValue);
}
}
notify = () => {
this.currentValue = /*arguments?*/;
for (var callback of this.callbacks) {
callback.apply(null, /*arguments?*/);
}
}
notify() {
/*this?*/.currentValue = arguments;
for (var callback of /*this?*/.callbacks) {
callback.apply(null, arguments);
}
}
}
I require a function in TypeScript where I can access both the `this` reference to access my fields and the arguments passed into the function. How can I achieve this?