我希望我的程序调用Contact类中的函数,该类具有一个名为getcontactdetails()的方法。
当对象存储在另一个类中时,如何调用该方法?
例如
显示应该是当我调用该函数时:
姓名:荷马
手机号码:98849959
var input = require("readline-sync");
var readline = require("readline-sync");
class Contact{
constructor(name,mobileNumber){
this.name=name;
this.mobileNumber=mobileNumber;
}
getcontactdetails(){
var s = 'Name: ' + this.name;
s += "\nMobile Number: " + this.mobileNumber;
return s;
}
}
class AddressBook{
constructor(){
this.myContact = []; //class property
this.myContact.push(new Contact("Homer", 98849959));
this.myContact.push(new Contact("Marge", 84774744));
this.myContact.push(new Contact("Lisa", 86994994));
this.myContact.push(new Contact("Maggie", 94775883));
this.myContact.push(new Contact("Bart", 88838848));
}
}
var myaddressBook = new AddressBook();
console.log(myaddressBook.getcontactdetails)
答案 0 :(得分:0)
您可以通过引用存储在myContact数组中的实例来调用它。
例如:
var myaddressBook = new AddressBook();
console.log(myaddressBook.myContact[0].getcontactdetails());