@descriptor

const log = (target, name, descriptor) => {
  //ES6实现AOP
  const _handler = descriptor.value;
  descriptor.value = function(){
    console.log(arguments);
   _handler.apply(this,arguments);
  };
  return descriptor;
}

class Math {
  @log
  add(a, b) {
    return a + b;
  }
  add2(a, b) {
    return a + b;
  }
  add3(a, b) {
    return a + b;
  }
}
//传统方式
!function(){
const _handler = Math.prototype.add2;
Math.prototype.add2 = function(){
    console.log(arguments);
   _handler.apply(this,arguments);
};
}();

const math = new Math();
math.add(1, 2);
math.add2(1, 2);

猜你喜欢

转载自blog.csdn.net/qq_39571197/article/details/82967051