Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to state and function invocations

Is it possible to listen to any function invocation or state change

I have a object that wrap another

function wrapper(origiObj){
   this.origObj = origObj;
}
var obj = wrapper(document);//this is an example
var obj = wrapper(db);//this is an example

now everytime someone tries to invoke obj.innerHTML or obj.query(..)

I would like to listen to that..

like image 789
ciochPep Avatar asked Mar 25 '26 23:03

ciochPep


1 Answers

Yes, it's possible: functions are easy, and properties has to be watched

function FlyingObject(obj){
    this.obj = obj;
    for(var p in obj){
        if(typeof obj[p] == 'function'){
            console.log(p);
            this[p] = function(){
                console.log("orig func");
            };
        }else{
            this.watch(p,function(){
                console.log("orig property");
            });
        }
    }
}
var obj = {
    f:function(a,b){ return a+b},
    m:1
};
var fo = new FlyingObject(obj);


fo.m = 5;

fo.f(1,4);

If your browser/node.js doesn't support Object.watch, check this out: Object.watch() for all browsers?

like image 99
DuduAlul Avatar answered Mar 28 '26 13:03

DuduAlul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!