banner 728x90

Watch out for object changes with JavaScript

In Mobile Web Development 34 views
banner 728x90

Looking at changes to the property of an object has always been a much requested task; Over the years, many discs have been used to listen to object changes. Nowadays we have better methods to listen to object changes: the proxy API. Sindre Sorhos has made on-change, a small JavaScript tool that lets you play for changes on JavaScript objects and array & # 39; s!

Change JavaScript

Because the replacement code is so small, here it is in its full glory:

& # 39; Strictly use & # 39;

module.exports = (object, onChange) => ;

The onChange function returns a proxy that you can use to modify an object.

Using on-change

Indicate at Change the object you want to spy on and a function for changing handlers:

let j = ;

// Save to server when saving
let changeable = onChange (j, () => save (j));

Then use that proxy to change properties and receive notifications of changes:


// Make a change that would cause changes
changeable.a = 2;

// save () is triggered!

// j.a === 2

Note that the values ​​of the original object also change – that's the beauty of Proxy! Also note that when changing is not indicated which property is changed; the use case, as Sindre describes, is to store an object (to server, etc.) when a part of an object changes. You could also use ti for a small library with a rendering function, where the content is redisplayed when a property is changed!

This exchange library is really nice if you do not have to know which house has changed, only that something has changed.


banner 728x90
author
Author: 
    Script & Style Show: Episode 1: Marc Grabanski
    Script & Style Show: Episode 1: Marc Grabanski
    During this second episode of the Script
    AWS AppSync region expansion and new functions for API keys
    AWS AppSync region expansion and new functions for API keys
    AWS AppSync, a managed GraphQL service with
    What is the best time to involve your mobile app users?
    What is the best time to involve your mobile app users?
    When talking to customers about mobile analysis,
    How to make a Twitter bot with Node.js
    How to make a Twitter bot with Node.js
    Twitter bots have been in the news

    Leave a reply "Watch out for object changes with JavaScript"

    Must read×

    Top