[removed] How do constantly monitor variables value

前端 未结 6 1033
青春惊慌失措
青春惊慌失措 2021-02-14 08:35

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:49

    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;
    

提交回复
热议问题