I\'m wondering when I should use
Object.defineProperty
to create new properties for an object. I\'m aware that I\'m able to set things lik
For example, that's how Vue.js keeps track of changes in the data object:
When you pass a plain JavaScript object to a Vue instance as its
dataoption, Vue will walk through all of its properties and convert them togetter/settersusingObject.defineProperty. This is an ES5-only and un-shimmable feature, which is why Vue doesn’t support IE8 and below.The getter/setters are invisible to the user, but under the hood they enable Vue to perform dependency-tracking and change-notification when properties are accessed or modified.
[...]
Keep in mind that even a super slim and basic version of Vue.js would use something more than just Object.defineProperty, but the main functionality comes from it:
Here you can see an article where the author implements a minimal PoC version of something like Vue.js: https://medium.com/js-dojo/understand-vue-reactivity-implementation-step-by-step-599c3d51cd6c
And here a talk (in Spanish) where the speaker builds something similar while explaining reactivity in Vue.js: https://www.youtube.com/watch?v=axXwWU-L7RM