JS参数转发

在没有装饰器之前不方便。

可以用Reflect.apply。

cls = function f() {
  let obj = {};

  obj.show = function(a, b) {
    console.log(a + b);
  }

  return obj;
}

function trans(a, b) {
  let o = cls();
  Reflect.apply(o.show, o, [a, b]); // 关键是这里,apply函数会自动解开数组!
}

trans(1, 4);

  

猜你喜欢

转载自www.cnblogs.com/willaty/p/9399035.html