class PaymentStrategy {
static Card(user){
console.log(`${user} will pay with credit card`);
}
static Paypal(user) {
console.log(`${user} will pay with paypal`);
}
static COD(user) {
console.log(`${user} will pay with COD`);
}
}
class Payment {
constructor(strategy = 'Card') {
this.strategy = PaymentStrategy[strategy];
}
changeStrategy(newStrategy) {
this.strategy = PaymentStrategy[newStrategy];
console.log('**** Payment method changed ******');
}
showPaymentMethod(user) {
this.strategy(user);
}
}
const pm = new Payment();
pm.showPaymentMethod('Mike');
pm.showPaymentMethod('Paul');
pm.changeStrategy('Paypal');
pm.showPaymentMethod('Alex');
pm.showPaymentMethod('Mars');
Because I am coming for traditional languages like Pascal it is confusing for me how this.strategy
point to a methods Card
Paypal
and COD
?
Is it normal to access class methods like this: ```
PaymentStrategy[strategy]
or it should be PaymentStrategy(strategy)
?