You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!
Deprecation warning: Do not use watch() and unwatch()! These two methods were implemented only in Firefox prior to version 58, they're deprecated and removed in Firefox 58+. In addition, using watchpoints has a serious negative impact on performance, which is especially true when used on global objects, such as window. You can usually use setters and getters or proxies instead.
The watch() method watches for a property to be assigned a value and runs a function when that occurs.
Syntax
obj.watch(prop, handler)
Parameters
prop- The name of a property of the object on which you wish to monitor changes.
handler- A function to call when the specified property's value changes.
Return value
Description
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 newval (or by returning oldval).
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 Object.
The JavaScript debugger has functionality similar to that provided by this method, as well as other debugging options. For information on the debugger, see Venkman.
In Firefox, handler is only called from assignments in 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, window.location += '#myAnchor' will call myHandler.
Note: Calling watch() on an object for a specific property overrides any previous handler attached for that property.
Examples
Using watch and unwatch
var o = { p: 1 };
o.watch('p', function (id, oldval, newval) {
console.log('o.' + id + ' changed from ' + oldval + ' to ' + newval);
return newval;
});
o.p = 2;
o.p = 3;
delete o.p;
o.p = 4;
o.unwatch('p');
o.p = 5;
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
Using watch to validate an object's properties
You can use watch to test any assignment to an object's properties. This example ensures that every Person always has a valid name and an age between 0 and 200.
Person = function(name, age) {
this.watch('age', Person.prototype._isValidAssignment);
this.watch('name', Person.prototype._isValidAssignment);
this.name = name;
this.age = age;
};
Person.prototype.toString = function() {
return this.name + ', ' + this.age;
};
Person.prototype._isValidAssignment = function(id, oldval, newval) {
if (id === 'name' && (!newval || newval.length > 30)) {
throw new RangeError('invalid name for ' + this);
}
if (id === 'age' && (newval < 0 || newval > 200)) {
throw new RangeError('invalid age for ' + this);
}
return newval;
}
will = new Person('Will', 29);
console.log(will); // Will, 29
try {
will.name = '';
} catch (e) {
console.log(e);
}
try {
will.age = -4;
} catch (e) {
console.log(e);
}
This script displays the following:
Will, 29 RangeError: invalid name for Will, 29 RangeError: invalid age for Will, 29
Specifications
Not part of any specifications. Implemented in JavaScript 1.2.
Browser compatibility
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
watch | Chrome No support No | Edge No support No | Firefox No support 1 — 58 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android No support 4 — 58 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
Legend
- No support
- No support
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.
- Deprecated. Not for use in new websites.
- Deprecated. Not for use in new websites.
Compatibility notes
- This Polyfill offers
watchto all ES5 compatible browsers. - Using a
Proxyenables you do even deeper changes to how property assignments work. - Calling
watch()on theDocumentobject throws aTypeErrorsince Firefox 23 (bug 903332). This regression has been fixed with Firefox 27.