简体   繁体   中英

What is the difference between Object.observe and Object.watch

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?

There are many difference.

Microtasks vs synchronous callbacks

One difference is that Object.observe makes callbacks when you enter the message loop. Ie 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. Ie 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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM