Watch.js Object Observer

0.0.0 · abandoned · verified Sun Apr 19

Watch.js is a compact, browser-compatible JavaScript library designed to implement the Observer design pattern, allowing developers to monitor changes to JavaScript objects and their attributes without altering existing development methodologies. It enables watching single attributes, multiple attributes, or all attributes of an object, invoking a callback function upon detected mutations. The library, last updated around 9 years ago (with version 1.2.0 mentioned in its README but 0.0.0 on npm), primarily operates by polluting the global scope, exposing `watch` and `unwatch` functions, and a `WatchJS` object. While simple to integrate via a direct script inclusion, its reliance on global scope and direct object mutation for reactivity is largely superseded by modern JavaScript features like Proxies and reactive frameworks. It is considered abandoned, with no active maintenance or updates.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to add and trigger watchers for specific or all attributes on a JavaScript object using the global 'watch' function.

// Include Watch.js via a script tag in your HTML:
// <script src="https://raw.githubusercontent.com/melanke/Watch.JS/master/src/watch.js"></script>

// defining our object however we like
var myObject = {
	attribute1: "initial value",
	attribute2: 123
};

// defining a 'watcher' for a specific attribute
watch(myObject, "attribute1", function(prop, action, newValue, oldValue){
	console.log(`Attribute '${prop}' changed from '${oldValue}' to '${newValue}'. Action: ${action}`);
});

// defining a 'watcher' for all attributes on the object
watch(myObject, function(){
    console.log("Any attribute of myObject changed!");
});

// when changing the attribute, its watcher will be invoked
myObject.attribute1 = "new value";

// changing another attribute will invoke the general object watcher
myObject.attribute2 = 456;

view raw JSON →