Declarative programming techniques provide leverage and power to developers. The web platform has pioneered many – not the least of which is HTML. As the practice of application development evolves, it is important that primitives exist in the platform which allow developers to create and evolve robust declarative mechanisms independent of those provided directly by the platform.
A class of declarative mechanisms depends on discovering that ECMAScript values have changed. For example, UI frameworks often want to provide an ability to databind objects in a datamodel to UI elements. Likewise, domain objects, application logic and persistence strategies can often best be described in terms of constraints and relationships on data.
Today, ECMAScript code wishing to observe changes to objects typically either creates objects wrapping the real data or employs dirty-checking strategies for discovering changes. The first approach requires objects being observed to buy into the strategy, making the user model more complex and eroding composability of concerns. The second approach has poor algorithmic behavior, requiring work proportional the the number of objects observed to discover if any change has taken place. Both require increased working sets.
The desired characteristics of a solution are:
Object.observe allows for the direct observation of changes to ECMAScript objects. It allows an observer to receive a time-ordered sequence of change records which describe the set of changes which took place to the set of observed objects.
Changes to objects are directly observable and are described in terms of
A flexible system is provided via a “notifier” object associated with every observable object, which allows for:
Lastly, Array.observe allows for the efficient description of certain changes which may affect many index-valued properties as single “splice” change record.
Object.observe is a relatively significant and cross-cutting feature, as it