I was curious about JS Design Patterns and decided to try this website,
but on the Command Pattern section I got super confused. You can see the explanation in the link but for this post’s sake I have provided a code snippet below.
class OrderManager {
constructor() {
this.orders = [];
}
execute(command, ...args) {
return command.execute2(this.orders, ...args);
}
}
class Command {
constructor(executee) {
this.execute2 = executee;
}
}
function PlaceOrderCommand(order, id) {
return new Command(orders => {
orders.push(id);
console.log(`You have successfully ordered ${order} (${id})`);
});
}
const manager = new OrderManager();
manager.execute(new PlaceOrderCommand("Pad Thai", "1234"));
I am super confused, I thought I was good at JS but apparently I am not..
The questions in line are:
-
How can you call the manager.execute with only one argument, when in the definition there are two?!
-
Why call the function with new keyword like new PlaceOrderCommand(“Pad Thai”, “1234”)
-
How does execute2 call the arrow function when it is just assigned in the constructor?!
all these things are super confusing and seem like magic to me..