追踪每个function 的调用,调用1个function 前输出1些调用的信息,对于调试很有帮助~~
实现的方法就是为每个function 增加1个__name__属性,再为function制作1个作为外壳的function。外壳function负责输出调试信息,再调用原函数。稍作修改就可以将信息输出为调用栈的格式,更容易看~恩。。
但还有些不足之处,譬如当多个对象的方法指向同1个function时,就不能正确地输出结果了- -。。
使用方法:
添加后面的实现代码后:
_root.q=function(){ trace(5);}// testtraceMessages(_root);_root.q();//output:[call] q (called by undefined)5
traceMessages实现代码:
traceMessages = function (pObj) {
arguments.callee.init(pObj);
arguments.callee.execute(pObj);
arguments.callee.exit();
}
//
o = traceMessages;
//
o.init = function (pObj) {
this.arrFoundedObjects = new Array();
//
this.setFounded(pObj);
}
//
o.exit = function () {
delete this.arrFoundedObject;
}
//
o.execute = function (pObj) {
ASSetPropFlags(pObj, [’__proto__’, ’prototype’], 0, 1);
for (var p in pObj) {
//
var strType = typeof(pObj[p]);
var obj = pObj[p];
//
if (!this.isContainer(obj)) continue;
if (!pObj.hasOwnProperty(p)) continue;
if (p == ’__func__’) continue;
if (this.isFounded(obj)) continue;
if (p == ’traceMessages’) continue;
//
if (strType == ’object’ || strType == ’movieclip’) {
this.setFounded(obj);
arguments.callee.call(this, obj); // recursion
continue;
}
//
this.setFounded(obj);
arguments.callee.call(this, obj); // recursion
//
var tmp = obj;
pObj[p] = function () {
trace(’[call] ’ + arguments.callee.__name__ +
’ (called by ’ + arguments.caller.__name__ + ’)’);
return arguments.callee.__func__.apply(this, arguments);
}
pObj[p].__name__ = p;
pObj[p].__func__ = tmp;
pObj[p].__func__.__name__ = p;
}
ASSetPropFlags(pObj, [’__proto__’, ’prototype’], 1, 0);
}
//
o.setFounded = function (pObj) {
this.arrFoundedObjects.push(pObj);
}
//
o.isFounded = function (pObj) {
for (var p in this.arrFoundedObjects) {
if (this.arrFoundedObjects[p] == pObj) {
return true;
}
}
return false;
}
//
o.isContainer = function (pObj) {
var strType = typeof(pObj);
return (strType == ’object’ || strType == ’function’ || strType == ’movieclip’);
}
//
delete o;
zz:
http://www.flashfanatiker.de/blog/archives/000024.html