Object.prototype.watch() - JavaScript | MDN Skip to main content Select language Skip to search MDN Web Docs Technologies HTML CSS JavaScript Graphics HTTP APIs / DOM Browser Extensions MathML References & Guides Learn web development Tutorials References Developer Guides Accessibility Game development ...more docs Feedback Get Firefox help Get web development help Join the MDN community Report a content problem Report a bug Sign in Github Search Search Close search Object.prototype.watch() Languages Español (es) Français (fr) æ¥æ¬èª (ja) Polski (pl) Ð ÑÑÑкий (ru) ä¸æ (ç®ä½) (zh-CN) æ£é«ä¸æ (ç¹é«) (zh-TW) Add a translation Edit Advanced Advanced History Print this article Jump to: Syntax Description Examples Specifications Browser compatibility Compatibility notes See also 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 undefined. 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 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. 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 The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. Update compatibility data on GitHub Desktop Mobile Server Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js watch DeprecatedNon-standard 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 watch to all ES5 compatible browsers. Using a Proxy enables you do even deeper changes to how property assignments work. Calling watch() on the Document object throws a TypeError since Firefox 23 (bug 903332). This regression has been fixed with Firefox 27. See also Object.unwatch() Object.observe() Document Tags and Contributors Tags: Debugging Deprecated JavaScript Method Object Obsolete Prototype Contributors to this page: mdnwebdocs-bot, fscholz, siddhantsoni, eduardoboucas, i80and, peter.kehl, Mingun, alanh, Scimonster, kohei.yoshino, kscarfone, Sheppy, torbasow, ethertank, trevorh, ziyunfei, grssam, BYK, jswisher, BlindWanderer, evilpie, dbruant, Joe Egan, paul.irish, Mgjbot, Sevenspade, Trinithis, Talisker, Hamstersoup, Yuichirou, Lcbarcellos, Enumera, Maian, Marcoos, Brendan, Dria Last updated by: mdnwebdocs-bot, Mar 23, 2019, 8:14:25 PM Web technology for developers JavaScript JavaScript reference Standard built-in objects Object Object.prototype.watch() Related Topics Standard built-in objects Object Properties Object.prototype Object.prototype.__count__ Object.prototype.__noSuchMethod__ Object.prototype.__parent__ Object.prototype.__proto__ Object.prototype.constructor Methods Object.assign() Object.create() Object.defineProperties() Object.defineProperty() Object.entries() Object.freeze() Object.fromEntries() Object.getNotifier() Object.getOwnPropertyDescriptor() Object.getOwnPropertyDescriptors() Object.getOwnPropertyNames() Object.getOwnPropertySymbols() Object.getPrototypeOf() Object.is() Object.isExtensible() Object.isFrozen() Object.isSealed() Object.keys() Object.observe() Object.preventExtensions() Object.prototype.__defineGetter__() Object.prototype.__defineSetter__() Object.prototype.__lookupGetter__() Object.prototype.__lookupSetter__() Object.prototype.eval() Object.prototype.hasOwnProperty() Object.prototype.isPrototypeOf() Object.prototype.propertyIsEnumerable() Object.prototype.toLocaleString() Object.prototype.toSource() Object.prototype.toString() Object.prototype.unwatch() Object.prototype.valueOf() Object.prototype.watch() Object.seal() Object.setPrototypeOf() Object.unobserve() Object.values() Inheritance: Function Properties Function.arguments Function.arity Function.caller Function.displayName Function.length Function.prototype Function.prototype.name Methods Function.prototype.apply() Function.prototype.bind() Function.prototype.call() Function.prototype.isGenerator() Function.prototype.toSource() Function.prototype.toString() Learn the best of web development Get the latest and greatest from MDN delivered straight to your inbox. E-mail I'm okay with Mozilla handling my info as explained in this Privacy Policy. Sign up now Thanks! Please check your inbox to confirm your subscription. If you havenât previously confirmed a subscription to a Mozilla-related newsletter you may have to do so. Please check your inbox or your spam filter for an email from us. Hide Newsletter Sign-up MDN Survey Help us understand the top 10 needs of Web developers and designers. Take the survey Minimize
banner Close
banner MDN Web Docs MDN Web Technologies Learn Web Development About MDN Feedback Twitter GitHub Mozilla Mozilla About Contact Us Firefox Twitter Instagram Other languages: English (US) (en-US) Español (es) Français (fr) æ¥æ¬èª (ja) Polski (pl) Ð ÑÑÑкий (ru) ä¸æ (ç®ä½) (zh-CN) æ£é«ä¸æ (ç¹é«) (zh-TW) Go Terms Privacy Cookies © 2005-2019 Mozilla and individual contributors. Content is available under these licenses.