[removed] How do constantly monitor variables value

前端 未结 6 2401
无人共我
无人共我 2021-02-14 08:36

How do I constantly check a variables value. For example:

if(variable == \'value\'){
    dosomething();
}

This would work if I constantly loope

6条回答
  •  深忆病人
    2021-02-14 08:47

    Object.defineProperty(Object.prototype, 'watch', {
        value: function(prop, handler){
            var setter = function(val){
                return val = handler.call(this, val);
            };
            Object.defineProperty(this, prop, {
                set: setter
            });
        }
    });
    

    How to use:

    var obj = {};
    
    obj.watch('prop', function(value){
        console.log('wow!',value);
    });
    
    obj.prop = 3;
    

提交回复
热议问题