Typescript 如何 Return Class 来自嵌套对象的函数引用 Getter

Typescript How To Return Class Function Reference From Nested Object Getter

我正在编写一个服务 class,其中包含一组实用函数。不是将所有函数公开为 public,而是将函数连接到一系列 public 对象中,这些对象旨在提供访问它们的逻辑流。这是一个例子:

class MyUtilService {
    public exposedObject = {
        get prop1() {
            return this.utilFunction1;
        },
        get prop2() {
            return this.utilFunction2;
        }
    };

    private utilFunction1() {//...}
    private utilFunction2() {//...}
}

问题是 getter 中的 this 对象指的是 exposedObject 而不是 class(正如预期的那样)。目标是像这样在应用程序的其他地方访问这些功能:

myUtilService.exposedObject.prop1() // Want this to execute utilFunction1

这里的另一个愿望是在使用时维护 utilFunction1 中的键入信息。我以前使用 .bind(this) 而不是 getter,但这阻止了任何键入信息的传递。 Object.defineProperty 也会发生同样的事情。关于如何让它工作有什么想法吗?

注意事项: 使用 TS 2.5.x 和 Angular 5.x

编辑: 我正在添加一些额外的信息,因为问题的核心在我最初的 post 中并不清楚。当其中一个私有函数使用服务中的另一个函数时,就会出现主要问题。考虑这段代码:

class MyUtilClass {
    public exposedObject = {
        prop1: this.utilFunction1
    };

    private utilFunction1() {
        this.utilFunction2();
    }
    private utilFunction2() {//...}
}

// Want this to execute utilFunction1 which executes utilFunction2
someService.exposedObject.prop1()

utilFunction2 在 utilFunction1 内部使用,但是当调用 myUtilClass.exposedObject.prop1() 时,未定义 this.utilFunction2 因为 this 上下文来自调用者(同样,正如预期的那样).这可以用 prop1: this.utilFunction1.bind(this) 来克服,但是你会丢失函数的输入信息。这是一个难题。这就是为什么我沿着嵌套 getter 的路线走下去的原因,但这在 this 上下文中有其自身的问题。

考虑到您要实现的目标,这应该对您有用。

class MyUtilService {
    public exposedObject = {
        prop1:this.prop1,
        prop2:this.prop2
    }
    private get prop1(){
        return this.utilFunction1();
    }
    private get prop2() {
        return this.utilFunction2();
    }

    private utilFunction1() {}
    private utilFunction2() {}
}

let utilityservice = new MyUtilService();
utilityservice.exposedObject.prop2;

TypeScript 确实是 JavaScript 的幕后黑手。您的 class 在执行之前被转换为 javascript 函数。 例如,当您使用 get 或 set 关键字定义函数时,您不使用 ():

o = {
  get number() {return 5;},
  set number(i) {this.myNumber = i;}
}
result = o.number;
o.number = 10;

每个对象 {} 都有自己的上下文 this

我什至不相信你所期望的是可能的。我玩了一下你的 class。我能够将外部上下文传递给内部对象,但 privet 方法仍然可用 public。似乎所有 TypeScript classes 都是如此,与 js 一样,所有成员都是 public。我是 TypeScript 的新手,只是想展示原型(TS 和 JS)与经典 OOP

的不同之处
class MyUtilService {
  public exposedObject: Object;
  constructor() {

    class ExposedObject {
      constructor(private parent: MyUtilService) {}
      get prop1() {
          console.log(this);
          return this.parent.utilFunction1();
      }
      get prop2() {
          return;
      }
    }
    this.exposedObject = new ExposedObject(this);
  }


  private utilFunction1() { return 'uf'; }
  private utilFunction2() {}
}

const u = new MyUtilService();
const value = u.exposedObject.prop1;
console.log(value);
console.log(u.utilFunction1());

好的,这就是我最终解决这个问题的方法。使用与之前相同的示例:

let _otherService: OtherService;

export class MyUtilClass {

    constructor(private otherService: OtherService) {
        _otherService = otherService;
    }

    public exposedObject = {
        prop1: utilFunction1
    };
}

function utilFunction1() {
    utilFunction2();
}
function utilFunction2() {
    _otherService.doSomething();
}

我最终在 class 的 外部 定义了效用函数。然后 this 不再有问题。这些函数可以相互使用,我什至可以通过构造函数公开注入的服务供它们引用(可能是因为在这些函数中的任何一个可用之前实例化了服务)。并且类型信息也是通过exposedObject继承的。谢谢大家的建议!