JavaScript, prior to ES6 (ECMAScript 2015), relied on constructor functions to mimic object-oriented concepts. These functions act as blueprints fo...
For further actions, you may consider blocking this person and/or reporting abuse
Hi Sandeep, You asked "What are" and "How to" but missed out asking "Why"? I suspect the only reason most people use classes is because they have yet to discover there are better ways, just as advised in the Gang of Four.
JS is still only 'emulating' classes using prototypes. The
Class
syntax that was introduced is just sugar over the existing prototype stuff - which is still being used underneath.Hey @jonrandy ,
Spot on about classes being fancy talk for prototypes! But they definitely have some perks, like:
Clearer Code: The class syntax makes your objects way easier to read and understand. No more squinting at prototype chains!
Better Organization: Classes keep everything nice and tidy by bundling data and methods together. Less code headaches for you!
OOP Magic: Classes make it a breeze to use those awesome object-oriented patterns like inheritance and polymorphism. 🪄
Overall, classes are like a super friendly way to work with objects in JavaScript.
Clearer if you are used to class-based inheritance. This kind of thing is completely subjective
Hi @jonrandy, Is it still true to say "JS classes are just syntactic sugar on JS prototypes", I am not so sure?
I query the assertion following the introduction of private properties as I do not think these can be used with or have an idiomatic equivalent for prototypes.
As stated on the MDN page "Private properties were not native to the language before this syntax existed. In prototypal inheritance, its behavior may be emulated..."
Regards, Tracy
*samaj me kuxh ni aya par sun ke accha laga *
Let me translate for those who don't speak Hindi(India's very and most loved official language):
Could not understand anything, but it sounds good.
Hopefully now everyone knows what this means, very popular statement in India BTW.
Informative article ,specially liked the humour .would be awesome if you highlight the main keypoints using bold/italic fonts.
Also,shouldnt classname start with uppercase letter as per convention?
Hey @neerajsharma2000,
Thanks for the highlighting it, we'll be updating it, was a typo lol 😝😝
It's nice Article, Keep writing stuffs you learn new.
We'll do that 😁😁😁