View profile

What Vue and Mongoose Have in Common

Vue is a frontend framework for building interactive browser apps. Mongoose is a backend framework/li
What Vue and Mongoose Have in Common
By Mastering JavaScript Tutorials • Issue #10 • View online
Vue is a frontend framework for building interactive browser apps. Mongoose is a backend framework/library for building APIs and backend services on top of MongoDB. However, they share a common mechanism for change tracking: the Object.defineProperty() function.
Take a look at the Vue docs’ notes on reactivity. The general idea is that Vue components loop over every own property in the component’s `data` object, and uses Object.defineProperty() to define a getter/setter on that property.

Vue will use Object.defineProperty() to watch for updates on the 'name' property
Vue will use Object.defineProperty() to watch for updates on the 'name' property
Mongoose takes a similar approach with models and schemas. Given a schema, Mongoose uses Object.defineProperty() to define a getter/setter for every path in the schema.
Mongoose will use Object.defineProperty() to watch for updates on 'name' and 'age'
Mongoose will use Object.defineProperty() to watch for updates on 'name' and 'age'
That’s because Object.defineProperty() is the only way to watch for changes without asking the user to explicitly define getters and setters themselves. This means Vue and Mongoose can be notified of changes as they happen, rather than diffing after the fact, which is what Angular and React do.
Unfortunately, Object.defineProperty() was introduced in ES5 and is impossible to polyfill in pre-ES5 browsers like Internet Explorer 8. So while Object.defineProperty() provides reactivity without requiring explicit notification and diffing, it won’t work for legacy React projects targeting older browsers.
This Week's Tutorials
Did you enjoy this issue?
Mastering JavaScript Tutorials

Mastering JavaScript Tutorials

If you don't want these updates anymore, please unsubscribe here
If you were forwarded this newsletter and you like it, you can subscribe here
Powered by Revue