JSFiddle - React, Tailwind, and code Playground
JavaScript
function MonitoredArray() {
this.onpush = null;
}
MonitoredArray.prototype = [];
MonitoredArray.prototype.nativepush = MonitoredArray.prototype.push;
MonitoredArray.prototype.push = function(x) {
this.nativepush(x);
if (typeof this.onpush == "function")
this.onpush(x);
};
var a = new MonitoredArray();
a.onpush = function(x) {
console.log("pushed", x);
};
a.push(1);
a.push(2);
console.log(a, a.length);