IMO javascript is a great language because you can use it functionally, or in a basic (non-class) manner, pretty effectively.
However, for anyone who needs to write code (libraries, tools, components, etc.) that will be used by multiple programmers, classes become very important. In agile shops, you will find that you might be able to whip out some pretty slick (non-class-based) code, but it's unlikely to survive a thorough code review process.
Classes help to define strict interfaces that are easy to read and use. Using the class based get and set keywords, you can easily validate and sanitize class properties, as well as perform business logic.
Additionally, using a well-thought out class constructor can make it easy to clone an object instance.
Defining the toJSON method on a class also allows you to precisely control the serialization of your class as well.
Here is a contrived example:
classCar{constructor(car=null){//can define non-public (implied) propertiesthis._licensePlate='';this._speed=0;this._direction=0;this._lightsOn=false;//take an instance of a Car as a constructor argumentif(car){for(letkincar){if(!car.hasOwnProperty(k)||this[k]===undefined)continue;this[k]=car[k]||this[k];}}}//get & set to control the property valuessetspeed(value){letv=+value;if(isNaN(v)){console.warn('property speed must typof number');return;}this._speed=v;}getspeed(){returnthis._speed;}setdirection(value){letd=+value;if(isNaN(v)||(d<0||d>360)){console.warn('property direction must be typeof number between 0 and 360');return;}this._direction=d;}getdirection(){returnthis._direction;}setlightsOn(value){this._lightsOn=!(!value||value==="false");}getlightsOn(){returnthis._lightsOn;}setlicensePlate(value){//sanitize the input to remove illegal charactersthis._licensePlate=(value||'').replace(/[^a-zA-Z0-9]/g,'');}getlicensePlate(){//guarantee uniformity in license platesreturnthis._licensePlate.toUpperCase();}//computed property. This is not serializedgetisSpeeding(){return (this.speed>55);}toggleLights(){this._lightsOn=!this._lightsOn;}//JSON.stringify will automatically use this function to serialize the classtoJSON(){return{licensePlate:this.licensePlate,speed:this.speed,direction:this.direction,lightsOn:this.lightsOn};}//super cheap way to clone an objectcopy(){returnnewCar(this.toJSON);}}
Model things based on what they do, rather than what they are.
Modelling your software according to what things do instead of what they are, gives you the benefits of classical inheritance without the downsides - the ability to reuse properties/behaviour while keeping your code adaptive to changing requirements.
IMO javascript is a great language because you can use it functionally, or in a basic (non-class) manner, pretty effectively.
However, for anyone who needs to write code (libraries, tools, components, etc.) that will be used by multiple programmers, classes become very important. In agile shops, you will find that you might be able to whip out some pretty slick (non-class-based) code, but it's unlikely to survive a thorough code review process.
Classes help to define strict interfaces that are easy to read and use. Using the class based
get
andset
keywords, you can easily validate and sanitize class properties, as well as perform business logic.Additionally, using a well-thought out class constructor can make it easy to clone an object instance.
Defining the
toJSON
method on a class also allows you to precisely control the serialization of your class as well.Here is a contrived example:
Model things based on what they do, rather than what they are.
Modelling your software according to what things do instead of what they are, gives you the benefits of classical inheritance without the downsides - the ability to reuse properties/behaviour while keeping your code adaptive to changing requirements.
I feel like this is such a messy way to read and understand code, compared to just exporting a module with the functions/variables you want public.
Familiarity Bias
Everyone has a bias for what is familiar.
You can also create a function to keep track of your context and disminish the amount of side-effects using a more functional approach.