DEV Community

Discussion on: What is Polymorphism? - Explaining Polymorphism to a Five Year Old

Collapse
lukeshiru profile image
LUKESHIRU • Edited

Just to be clear, you can achieve "polymorphism" without classes in JavaScript, so is not like you need classes to have polymorphism. Your example reimplemented without classes could look something like this:

const createAircraft = ({ wings = 2, engines = 2, wheels = 3 } = {}) => ({
    start: () => console.log(`turning on my ${engines} engines!`),
    stand: () => console.log(`standing on my ${wheels} tires!`),
    run: () => console.log(`moving down the runway on my ${wheels} tires!`),
    fly: () => console.log(`soaring the sky on my ${wings} wings!`)
});

const createHelicopter = ({ engines = 1, wheels = 0, propellers = 2 } = {}) => {
    const aircraft = createAircraft({ engines, wheels });
    const propel = () => console.log(`spinning my ${propellers} propellers!`);
    const lift = () => {
        aircraft.stand();
        aircraft.start();
        propel();
        console.log("slowly climbing up thanks to my spinning propellers!");
    };

    return { ...aircraft, propel, lift };
};
Enter fullscreen mode Exit fullscreen mode

Cheers!

Collapse
ubahthebuilder profile image
Kingsley Ubah Author

Thanks for the insight.

Collapse
snickdx profile image
Nicholas Mendez

Because you can't typecast helicopter objects as aircraft objects, I'd argue your example cannot apply polymorphism in the true sense of the term. Inheritance is the key a was shown in the OP while what you showed composition.

Collapse
lukeshiru profile image
LUKESHIRU

You can use helicopter in any place you use aircraft, because they have the same methods (as noted by ...aircraft). You can see it working in TS as well if you want, here. It is closer to composition, but the comment mainly highlights that polymorphism is not something that you can only achieve with classes.

Collapse
machineno15 profile image
Tanvir Shaikh • Edited

i understood some part of this, can you please explain me what's happening here ? that will be helpful

Collapse
lukeshiru profile image
LUKESHIRU • Edited

Sure!

  • First: createAircraft takes a single argument, which is an object with the values, but you can pass some and omit others, or omit them all and leave the defaults if you want. The good thing about it being an object, is that you can easily skip some arguments, or use whatever order you prefer, and it still works. Adding new arguments is a piece of cake as well.
createAircraft(); // Leaving all the defaults valid
createAircraft({ wings: 1 }); // Setting only 1 property valid as well
createAircraft(({ wings = 1, engines = 1, wheels = 4 }); // Setting all properties... valid!
Enter fullscreen mode Exit fullscreen mode
  • Second: Because createAircraft returns an object with some functions in it, you can create new objects from it, so when createHelicopter does { ...aircraft }, is adding all the functions returned by createAircraft to its own output object, and then some more (propel and lift).
  • Third: This example could be way more simple if the functions were outside of createAircraft, but the idea was to show that you can "replicate" what you can do with classes, without them.
  • Forth: Ideally, every method should actually return the string instead of logging it, but I tried to replicate the original example.

Bonus: Here's how you can use the code above, and what's the output:

createAircraft();
/*
Returns:
{
    start: () => console.log(`turning on my 2 engines!`),
    stand: () => console.log(`standing on my 3 tires!`),
    run: () => console.log(`moving down the runway on my 3 tires!`),
    fly: () => console.log(`soaring the sky on my 2 wings!`)
}
*/
createHelicopter({ propellers: 2 });
/*
Returns:
{
    start: () => console.log(`turning on my 2 engines!`),
    stand: () => console.log(`standing on my 3 tires!`),
    run: () => console.log(`moving down the runway on my 3 tires!`),
    fly: () => console.log(`soaring the sky on my 2 wings!`),
    propel: () => console.log(`spinning my 2 propellers!`),
    lift: () => {
        aircraft.stand(); // Will log `turning on my 2 engines!`
        aircraft.start(); // Will log `standing on my 3 tires!`
        propel(); // Will log `spinning my 2 propellers!`
        console.log("slowly climbing up thanks to my spinning propellers!");
    };
}
*/
Enter fullscreen mode Exit fullscreen mode

Let me know if there is something else I can help you with :D