Sign in
Log inSign up
if-else
if-else
#if-else
41 articles
Popular this week 🔥
Thai Tran
Maxi Contieri⭐⭐⭐
Tony Tin Nguyen
Avatar
Maxi Contieri⭐⭐⭐ and 17 others are discussing this18 people are discussing this
raddevus
That's a nice well-written article with good points. Software Dev Team Lead Here's a Software Dev Team Lead's way of doing this: No need to even determine the type. Instead the type implements the correct method for itself & there is never a need to even know the base type. Strategy pattern. 😎🤓 Try The Code Try the code out and see it run at: https://stackblitz.com/edit/typescript-xibe8j?file=index.ts interface Animal{ Speak(); } class Cat implements Animal{ Speak(){ alert("meow"); } } class Dog implements Animal{ Speak(){ alert("woof"); } } class Lion implements Animal{ Speak(){ alert("roar!"); } } let a : Animal = new Dog(); a.Speak(); // woof let allAnimals = []; allAnimals.push(new Dog()); allAnimals.push(new Lion()); allAnimals.push(new Cat()); allAnimals.push(new Lion()); allAnimals.forEach( a => a.Speak());
9
Discuss

·

13 likes

·

73 reads