Skip to content
This repository has been archived by the owner on Dec 17, 2021. It is now read-only.

Latest commit

 

History

History
52 lines (37 loc) · 1.75 KB

closure.md

File metadata and controls

52 lines (37 loc) · 1.75 KB

Observer.closure()

This function provides a observer-aware context under which to execute code that could potentially mutate an observed object. Under this context, all mutations made to the object will be detected and observers that may be bound to the object will be notified.

Syntax

// Establish a closure on one or more objects
Observer.closure(callback, object1[, object2[, ]]);

Parameters

  • callback - the closure's callback function. This function recieves the listed objects into the closure in the order they were listed.
  • object1 - an object or array to observe.

Return Value

undefined

Usage

// The observed object/array
let arr = [], obj = {};
Observer.observe(arr, changes => {
    console.log(changes);
});

// The closure
Observer.closure((arr, obj) => {
    arr.push('one');
    arr.push('two');
    arr.push('three');
    arr.push('four');
    arr.shift();
}, arr, obj);

The above operation above will notify our observer once for a set operation on the properties 0, 1, 2, length.

Notice that changes are detected and observers fired after the code runs. These changes are detected by comparing the state of the object before and after the transaction. Intermediate changes, therefore, do not get caught, and interceptors that may have been bound to the object don't get fired. (Compare Observer.proxy().)

Related Methods