日期:2014-05-16 浏览次数:20355 次
function trace(o, m) {
var original = o[m];
o[m] = function() {
console.log(new Date(), "Entering:", m);
var result = original.apply(this, arguments);
console.log(new Date(), "Entering:", m);
return result;
};
}
function trace(o, m) {
var original = o[m];
o[m] = function() {
console.log(new Date(), " [Trace] Entering method:", m, "()");
var result = original.apply(this, arguments);
console.log(new Date(), " [Trace] Leaving method:", m, "()");
return result;
};
}
// Define a method foo()
function foo() {
console.log(new Date(), " foo is called");
}
// Test foo() without trace
foo();
/* Console displays:
Fri Mar 15 12:00:00 UTC+0x00 2013 foo is called
*/
// Test foo() with trace. (注意;foo 是 window 的一个函数)
trace(window, "foo");
foo();
/* Console displays:
Fri Mar 15 12:00:00 UTC+0x00 2013 [Trace] Entering method:foo()
Fri Mar 15 12:00:00 UTC+0x00 2013 foo is called
Fri Mar 15 12:00:00 UTC+0x00 2013 [Trace] Leaving method:foo()
*/