How it works?

The $.live() function is made to toggle a function when a value changes in an array. This function was largely inspired by the Object.prototype.watch() function that isn't available for all modern browsers 😊. So, it simply works by watching all element in an array.

⚠️ Our implementation of Object.prototype.watch() is not perfect, there is still some bugs. By the way, don't use it for the window variable. That's why the push method's live implementation doesn't work with window

How to use it?

This function will take 2 arguments: $.live(watched, callback). The callback will have 4 arguments: callback(obj, prop, newVal, oldVal)

Now, let's create an array that, when modified, will tell the modified value. (Yes, I know that this have no useful purpose, except maybe if you're dealing with security 🚨 stuff.)

JS:

// declaring the dog object
var dog = {"cute": "yep!", "dangerous": false};
// Creating the function that will tell modified value
$.live(dog, function(obj, prop, newVal, oldVal){ console.log("dog was modified, "+ prop +" was "+oldVal+" and has been set to "+newVal) });
// Testing
dog.cute = "nop!"
dog.dangerous = true;

⚠️ Questions?

Don't hesitate to ask your questions ⁉️ in the issue part 😁

results matching ""

    No results matching ""