onlycode.org

javascript
jquery
html
css
angularjs
node.js
ajax
html5
php
json
arrays
google-chrome
dom
regex
reactjs
ecmascript-6
javascript-events

your's history:
listener for property va...

Listener for property value changes in a javascript object, just only code

javascript-events | browser | javascript

edit
obj.watch('count', function(id, oldval, newval) {
    var val = parseInt(newval, 10);
    if(isNaN(val)) return oldval;
    return val;
});
obj.watch('name', function(id, oldval, newval) {
    return newval.substr(0, 20);
});

28
Prestaul, 01.07.2009 00:43




edit
var o = {}
Object.defineProperty(o, "p", {
    value:1,
    writable:true,
    onchange:function(e){
        console.log("o." + e.target + " changed from " + e.previousValue + " to " + e.returnValue);
    }
})

26
Jonatas Walker, 16.11.2015 20:52




edit
Object.prototype.startWatch = function (onWatch) {

    var self = this;

    if (!self.watchTask) {
        self.oldValues = [];

        for (var propName in self) {
            self.oldValues[propName] = self[propName];
        }


        self.watchTask = setInterval(function () {
            for (var propName in self) {
                var propValue = self[propName];
                if (typeof (propValue) != 'function') {


                    var oldValue = self.oldValues[propName];

                    if (propValue != oldValue) {
                        self.oldValues[propName] = propValue;

                        onWatch({ obj: self, propName: propName, oldValue: oldValue, newValue: propValue });

                    }

                }
            }
        }, 1);
    }



}

var o = { a: 1, b: 2 };

o.startWatch(function (e) {
    console.log("property changed: " + e.propName);
    console.log("old value: " + e.oldValue);
    console.log("new value: " + e.newValue);
});

25
Taner, 28.08.2018 17:41

Add your just only code

your name:    



If you need full text click here

Some similar links Listener for property value changes in a javascript object ↓:
Accessing an object's property from an event listener call in Javascript

Can a JavaScript object property refer to another property of the same object? [duplicate]

How to set object property (of object property of..) given its string name in JavaScript?

Assigning property to JS object using another objects property value as the property name



This project is created for those who just need only code, wihout maaaaaaaaaaany words.