使用AOP进行方法调用之前和之后的Javascript控制台输出 [英] Javascript console output before and after method call with AOP

查看:191
本文介绍了使用AOP进行方法调用之前和之后的Javascript控制台输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想衡量方法的计算时间.

I would like to measure the computing time of methods.

一种不错的方法是(如何性能测试JavaScript代码?)与console.time('Function #1');console.timeEnd('Function #1');

A nice way is (How do you performance test JavaScript code?) with console.time('Function #1'); and console.timeEnd('Function #1');

我的想法是将这些控制台输出添加到生命周期方法中.在这种情况下,使用createContent:funtion(){};方法之类的SAPUI5.

My idea is to add these console outputs on lifecycle-methods. In this case using SAPUI5 like createContent:funtion(){}; methods.

这对于使用before()after()进行时间计数的AOP应该是可能的.

This should be possible with AOP using before() and after() to runt the time counting.

您会建议哪个AOP框架以及如何在需要自动修改标识字符串功能#1" 的情况下实现它?

Which AOP framework would you suggest and how to implement it with the need of modifying the identification string "Function #1" automatically?

推荐答案

这对于使用AOP使用before()和after()进行计时的情况应该是可能的.

This should be possible with AOP using before() and after() to runt the time counting.

正如已经提到的,确实不需要真正的面向方面的编程 为了解决JavaScript中的此类任务.但是这种语言可能值得更标准化 除了已有的bind方法之外,还有方法修饰符.

As it already got mentioned, one really is not in need of real Aspect-oriented Programming in order to solve such tasks in JavaScript. But this language might deserve some more standardized method-modifiers in addition to the already existing bind method.

请回覆我有关此事的2篇最新文章:

Please check back with my 2 most recent posts on this matter:

  • sandwich pattern in javascript code
  • Can you alter a Javascript function after declaring it?

...以及如何在需要自动修改标识字符串功能#1"的情况下实现该功能?

... and how to implement it with the need of modifying the identification string "Function #1" automatically?

不需要,因为控制台的time/timeEnd功能仅需具有 相同的用于测量时间的入口和出口点(例如秒表的开始/停止触发器). 因此,人们完全可以理解当前正在运行/测量的功能/方法的引用.

One does not need to since the console's time / timeEnd functionality only has to have identical entry and exit points for measuring time (like the start/stop trigger of a stopwatch). So one gets along with exactly the reference of the function/method one is currently running/measuring.

为了解决给定的任务,我只建议around而不是before和 前者的after产生较少的开销.下一个代码块示例性地显示了 可能的原型实现.这也是后续示例的基础 最终可以解决OP的任务.

In order to solve the given task I will suggest around only instead of both before and after for the former generates less overhead. The next code block exemplarily shows a possible prototypal implementation. It also is the base for the afterwards following example that finally might solve the OP's task.

(function (Function) {
  var
    isFunction = function (type) {
      return (
           (typeof type == "function")
        && (typeof type.call == "function")
        && (typeof type.apply == "function")
      );
    },
    getSanitizedTarget = function (target) {
      return ((target != null) && target) || null;
    }
  ;
  Function.prototype.around = function (handler, target) { // [around]
    target  = getSanitizedTarget(target);

    var proceed = this;
    return (isFunction(handler) && isFunction(proceed) && function () {

      return handler.call(target, proceed, handler, arguments);

    }) || proceed;
  };
}(Function));

下一个示例考虑了方法修改本质上所依赖的 绑定到对象的功能.这不仅仅是函数包装.为了 为了不丢失正在操作方法的 context ,必须委托 context / 在所有操作中都以target的形式传递.

The next example takes into account that method-modification essentially relies on functionality that is bound to an object. It is not just function wrapping. In order to not loose the context a method is operating on, context has to be delegated / passed around as target throughout all operations.

为此,示例未修改calculate,因为它未绑定到对象 但它会修改trigger.

For this the example does not modify calculate since it is not bound to an object but it modifies trigger instead.

var testObject = {

  calculate: function (hugeInteger) {
    var
      i = hugeInteger,
      k = 0
    ;
    while (i--) {
      k++;
    }
    return k;
  },
  trigger: function (hugeInteger) {
    this.result = this.calculate(hugeInteger);
  },
  result: -1
};

console.log("testObject.result : ", testObject.result);

console.log("testObject.trigger(Math.pow(2, 26)) : ", testObject.trigger(Math.pow(2, 26))); // takes some time.
console.log("testObject.result : ", testObject.result);

console.log("testObject.someTrigger(0) : ", testObject.trigger(0)); // logs immediately after.
console.log("testObject.result : ", testObject.result);


testObject.trigger = testObject.trigger.around(function (proceed, interceptor, args) {

  // before:
  console.time(proceed);

  // proceed:
  proceed.apply(this, args);

  // after:
  console.timeEnd(proceed);

}, testObject); // omitting the 2nd argument - the [target] object - might break code that did work before.


console.log("testObject.trigger(Math.pow(2, 26)) : ", testObject.trigger(Math.pow(2, 26)));
console.log("testObject.result : ", testObject.result);

这篇关于使用AOP进行方法调用之前和之后的Javascript控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆