4

Object.watch: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/watch

Object.observe: http://wiki.ecmascript.org/doku.php?id=harmony:observe

They both seem do the same thing at a high level. What are the salient differences between them?

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
Transcendence
  • 2,226
  • 2
  • 18
  • 31
  • "Warning: Generally you should avoid using watch() and unwatch() when possible. These two methods are implemented only in Gecko, and they're intended primarily for debugging use" – Mike 'Pomax' Kamermans Oct 11 '13 at 20:53
  • Simply from reading the documentation in your own links: `watch` is Mozilla-only (well, Gecko-only), and not a standard. `observe` seems to be a planned specification with a superset of the same functionality, but harmony isn't a standard yet, so that makes `observe` vaporware. Once it's there, it looks like it will notice changes to object members like `watch` in addition to noticing creation and deletion of object members. – Scott Mermelstein Oct 11 '13 at 21:04
  • Thanks @Mike'Pomax'Kamermans, I didn't read that line at all. – Transcendence Oct 11 '13 at 23:45
  • @ScottMermelstein I suppose my real question is why didn't they just build off of watch since it already exists? – Transcendence Oct 11 '13 at 23:46
  • @Transcendence good thing you used sarcasm there, wouldn't want you to include that information in the post so people don't point out fairly important information from the articles you link to without indicating you saw that bit. Given that that's the difference, which is what your question was. – Mike 'Pomax' Kamermans Oct 12 '13 at 01:07
  • @Transcendence It's important to realize that MDN != w3c. Mozilla participates in the world wide web consortium, but is not the sole decider about what goes into the next version of an ECMAScript specification. I'm not posting this as an answer, because I'm too lazy to crawl through the w3c's blogs and other open discussions, but `watch` is not exactly mainstream - as Mike and you noted, it's decorated with big "do not use" signs. It's not exactly so well-used as to be a de facto standard. *Maybe* the mozilla folks will use it to make `observe` - once Harmony deos become the standard. – Scott Mermelstein Oct 13 '13 at 04:08

1 Answers1

2

There are many difference.

Microtasks vs synchronous callbacks

One difference is that Object.observe makes callbacks when you enter the message loop. I.e. many changes to the object results in a single callback with all changes rather than multiple callbacks.

When listening to changes on the DOM, the mutation events was replaced with mutation observers for the same reason. The microtask solution is simply more performant than the synchronous callback.

Standard vs non-standard

In addition, Object.observe is a suggested Ecmascript standard for Ecmascript 7. I.e. it is a proposed Javascript standard.

Intent

Object.observe is intended as a performant way to monitor changes to an entire object and the use case is expected to entail listening to many objects. This is a requirement for binding frameworks (i.e. client side templating) such as AngularJs and Polymer. Object.watch is more a Firefox feature to monitor a specific property and is sprung out of a debugger feature.

Jack Wester
  • 4,092
  • 2
  • 25
  • 43