我需要的是拦截从rootsope或任何子范围调用的任何函数。所以问题是,我需要跟踪我的应用程序上的所有东西,并将这些数据发送到外部分析服务。所以在伪代码中,我需要的是:
$rootScope.watch($rootScope,函数(事件)){ 分析(event.name,event.params); event.run();//继续将要发生的事情. });
希望这有助于理解我所需要的。我似乎抓不到(拦截)任何一个函数。
发布于 2014-05-29 20:06:36
我认为这是一个有趣的想法,于是我写了一些东西(http://jsfiddle.net/jgoemat/nbvRQ/1/):
// global stats
var stats = {};
// function that will be called and will update stats with timing
function collectStats(f) {
var s = stats[f.toString()];
if (s === undefined) {
s = { name: f.name, count: 0, time: 0 };
stats[f.toString()] = s;
}
var args = [];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
var start = performance.now();
var result = f.apply(this, args);
var ms = performance.now() - start;
s.count++;
s.time += ms;
return result;
}
// proxy returns a function that can be called like the original
// but that actually calls collectStats() with the function as
// an argument
function proxy(f) {
return collectStats.bind(null, f);
}你可以这样打电话:
// function to slowly square a number
function squared(a) {
var result = 0;
for (var i = 0; i < a; i++) {
for (var j = 0; j < a; j++) {
result++;
}
}
return result;
}
// proxy for the function that will collect timings
var f = proxy(squared);
// anonymous proxy for an identical function
var f2 = proxy(function(b) {
var result = 0;
for (var i = 0; i < b; i++) {
for (var j = 0; j < b; j++) {
result++;
}
}
return result;
});
var totals = [0, 0, 0];
for (var i = 9000; i < 9005; i++) {
totals[0] += f(i);
totals[1] += f2(i);
// call with inline function
totals[2] += proxy(function(a) { return a * a; })(i);
}https://stackoverflow.com/questions/23832449
复制相似问题