【发布时间】:2021-11-16 23:17:23
【问题描述】:
OOP Javascript 新手!我有 2 个课程,订单和客户。我有一个方法 getName()。我希望客户作为我在 Order 类中的属性之一。我已经为两者创建了对象,但似乎无法访问订单对象中的客户名称。
Customer.js
class Customer{
constructor(id, name, address,creditCard) {
this._id=id;
this._name=name;
this._address=address;
this._creditCard=creditCard;
}
getName(){
return this._name;
}
}
Order.js
class Order {
customer;
constructor(id, date) {
this._id = id;
this._date=date;
this.customer=new Customer();
}
getNameCustomer() {
return this.customer.getName();
}
}
data.js
const customer1 = new Customer(123,"John","123 E Cookie St",'xxxx xxxx xxxx 1223');
const order1= new Order(801,'January 12, 2021 01:15:00',customer1.getNameCustomer() );
let orders=[order1];
【问题讨论】:
-
您正在调用不存在的
customer1上的getNameCustomer方法。你应该使用customer1.getName()... -
如果不传递
Customer所需的数据,这将毫无意义this.customer = new Customer();。
标签: javascript class oop