How to watch for array changes?
问题 In Javascript, is there a way to be notified when an array is modified using push, pop, shift or index-based assignment? I want something that would fire an event that I could handle. I know about the watch() functionality in SpiderMonkey, but that only works when the entire variable is set to something else. 回答1: There are a few options... 1. Override the push method Going the quick and dirty route, you could override the push() method for your array 1 : Object.defineProperty(myArray, "push"