Watches for a property to be assigned a value and runs a function when that occurs.
|Method of Object|
- The name of a property of the object.
- A function to call.
Watches for assignment to a property named
prop in this object, calling
handler(prop, oldval, newval) whenever
prop is set and storing the return value in that property. A watchpoint can filter (or nullify) the value assignment, by returning a modified
If you delete a property for which a watchpoint has been set, that watchpoint does not disappear. If you later recreate the property, the watchpoint is still in effect.
To remove a watchpoint, use the
unwatch method. By default, the
watch method is inherited by every object descended from
In NES 3.0 and 4.x,
handler is called from script as well as native code. In Firefox,
handler is only called from script, not from native code. For example,
window.watch('location', myHandler) will not call
myHandler if the user clicks a link to an anchor within the current document. However, the following code will call
window.location += '#myAnchor';.
This script displays the following:
o.p changed from 1 to 2 o.p changed from 2 to 3 o.p changed from undefined to 4