重写toString未调用

时间:2018-04-26 15:51:40

标签: javascript angular typescript

尝试调用以下课程的toString时,会调用Object.toString并将[object Object]打印到控制台。为什么Bankaccount.toString没有被调用?

export class Bankaccount {
  name: string;
  iban: string;
  bic: string;
  accountNumber: string;
  customerId: string;
  type: string;
  id?: number;

  constructor(name: string, iban: string, bic: string, accountNumber?: string, customerId?: string, type?: string, id?: number) {
    this.name = name;
    this.iban = iban;
    this.bic = bic;
    this.accountNumber = accountNumber;
    this.customerId = customerId;
    this.type = type;
    this.id = id;
  }

  toString = (): string => {
    return 'Bankaccount[Name: ' + this.name + ', IBAN: ' + this.iban + ', BIC: ' + this.bic + ', Typ: ' + this.type + ']';
  }

}

来电者/客户:

import {Bankaccount} from '../../models/model-interfaces';
... further imports ...

@Component({
  selector: 'ac-bankaccount-edit',
  templateUrl: './bankaccount-edit.component.html',
  styleUrls: ['./bankaccount-edit.component.css']
})
export class BankaccountEditComponent {

  bankaccount: Bankaccount = new Bankaccount('BankName', 'IBAN1234', 'BIC9876', '', '', 'PRIVATE');

  constructor() {}

  logBankaccount() {
    console.log('BA1: ' + this.bankaccount);
    console.log('BA2: ' + this.bankaccount.toString());
    console.log(`${this.bankaccount}`);
  }

}

0 个答案:

没有答案