首页 > 解决方案 > 类的属性作为类并访问父属性

问题描述

描述我在这里问的内容非常棘手,因此我将仅使用一个假设示例:

import { Address } from "./Address";

export class Customer {
  public firstName: string;
  public address: Address;

  public constructor(firstName: string, city: string) {
    this.firstName = firstName;
    this.address = new Address(city);
  }

}

export class Address {
  public city: string;

  public constructor(city: string) {
    this.city = city;
    // Can I access the Customer.firstName from here?
  }

}

根据我在Address上面的类中的评论,是否可以访问启动此address属性的类的属性(当然不将其作为 arg 发送)?在这种情况下,Customer实例。

希望这有意义吗?

标签: typescriptecmascript-6

解决方案


推荐阅读