Skip to content

Instantly share code, notes, and snippets.

@baybatu
Last active July 19, 2021 09:44
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save baybatu/992736452baf87b4d186 to your computer and use it in GitHub Desktop.
Save baybatu/992736452baf87b4d186 to your computer and use it in GitHub Desktop.
Attach event listener to Array push method call
var eventify = function(arr, callback) {
arr.push = function(e) {
Array.prototype.push.call(arr, e);
callback(arr);
};
};
var array = [1,2,3];
eventify(array, function(newArray) {
alert("new array length is:" + newArray.length);
});
array.push(4); // alerts new array length is: 4
array.push(5); // alerts new array length is: 5
@TeacherStijn
Copy link

Like! Thx 👍

@richyk1
Copy link

richyk1 commented Jul 8, 2021

Thanks for the quick snippet! Using it right now!

@nicojones
Copy link

Clever, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment