Last active
July 19, 2021 09:44
-
-
Save baybatu/992736452baf87b4d186 to your computer and use it in GitHub Desktop.
Attach event listener to Array push method call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Thanks for the quick snippet! Using it right now!
Clever, thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Like! Thx 👍