收听状态和函数调用 [英] Listen to state and function invocations
本文介绍了收听状态和函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以监听任何函数调用或状态更改
我有一个包装另一个的对象
函数包装器(origiObj){
this.origObj = origObj;
}
var obj = wrapper(document); //这是一个例子
var obj = wrapper(db); //这是一个例子
$ c $每当有人试图调用obj.innerHTML或obj.query(..)
时p>我想听听...... 解决方案是的,有可能:
函数很简单,属性必须被监视
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);
如果您的browser / node.js不支持Object.watch,请查看:
所有浏览器的Object.watch()?
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..
解决方案 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?
这篇关于收听状态和函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文