Embarking on an advanced React course has me tackling some preliminary tasks.
The task description lacks clarity on the expected outcome, leaving me uncertain about my results.
The task:
Part 1
To begin, create a constructor for object Client
with the argument name
.
This object should have attributes such as name
, orders
(an array to store orders), and a method addOrder(order)
(to add new orders to orders
)
Part 2
Next, construct a constructor for object Order
with arguments client
and number
.
This object should possess characteristics like client
(related to the client) and number
(order number)
Part 3
Verify your code by testing it with the following script:
const client1 = new Client("John");
const order1 = new Order(client1, "1");
const order2 = new Order(client1, "2");
client1.addOrder(order1);
client1.addOrder(order2);
console.table(client1.orders);
Upon reviewing my own code:
class Client {
constructor(name) {
this.name = name;
this.orders = [];
}
addOrder(order) {
this.orders.push(order);
}
}
class Order extends Client {
constructor(client, number) {
super(client);
this.number = number;
}
}
const client1 = new Client("John");
const order1 = new Order(client1, "1");
const order2 = new Order(client1, "2");
client1.addOrder(order1);
client1.addOrder(order2);
console.table(client1.orders);
The output displayed (which I suspect is incorrect, given that name
should be "John" and orders
arrays should not exist):
Array(2)
0: Order {name: Client, orders: Array(0), number: '1'}
1: Order {name: Client, orders: Array(0), number: '2'}