Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting change in a Javascript Object

Tags:

I found this gist to detect changes on specified fiels of a object : https://gist.github.com/3138469

But it bind an event only on one field.

Someone know a function or a tricks to detect change on an entire Javascript Object ?

like image 795
Unitech Avatar asked Jul 20 '12 11:07

Unitech


People also ask

How do you check if an object has a specific property in JavaScript?

We can check if a property exists in the object by checking if property !== undefined . In this example, it would return true because the name property does exist in the developer object.

What is EMP in JavaScript?

Here Emp can act as a class in JavaScript. The body of Emp acts as a constructor and is called as soon as we create an object of the class. Creating Objects of Emp Class. Using the following syntax we can create an object of the Emp class: var Emp1 = new Emp();


2 Answers

2019 Update: These days this can be achieved using Proxy API in a much more efficient manner. The on-change library uses the Proxy API behind the scene to make this even easier.

2012 Update: I've just noticed that the author of Watch.js is referencing a library with much broader browsers' support. MultiGetSet.JS

When I want to achieve this I usually use Watch.js, you can watch a whole object or one attribute.

like image 156
Adi Avatar answered Nov 09 '22 21:11

Adi


This is currently not possible, you can only define getters/setters for individual properties.

Yet, there is a draft for Proxy objects which could do that. Today, it is only supported in Firefox' Javascript 1.8.5.

like image 26
Bergi Avatar answered Nov 09 '22 21:11

Bergi