Professional Documents
Culture Documents
Factories Are STILL Better Than Classes in JavaScript - by GreekDataGuy - JavaScript in Plain English
Factories Are STILL Better Than Classes in JavaScript - by GreekDataGuy - JavaScript in Plain English
Follow
GreekDataGuy Follow
After writing, Why factories are better than classes in JS, I got a ton of
feedback and corrections.
Let’s take this opportunity to dig into those suggestions, for my own
knowledge, and for the benefit of anyone new to JavaScript.
For example, given a class, Car , we create an instance with new , and call
the drive method.
class Car {
constructor(maxSpeed){
this.maxSpeed = maxSpeed;
}
drive(){
console.log(`driving ${this.maxSpeed} mph!`)
}
honk(){
console.log(`honk!!!`)
}
}
car1.maxSpeed = 300
car1.drive()
#=> "driving 300 mph!"
car1.drive = function(){console.log('parked')}
car1.drive()
#=> "parked"
Editing properties and methods from the outside seems like undesirable
behaviour in most cases.
To be fair, this is how JavaScript was intended to behave. But that said,
intended behaviour doesn’t always equal “great” or ideal behaviour.
class Car {
constructor(maxSpeed){
this.defMaxSpeed = maxSpeed;
}
get maxSpeed(){
return this.defMaxSpeed
}
drive(){
console.log(`driving ${this.maxSpeed} mph!`)
}
honk(){
console.log(`honk!!!`)
}
}
Notice we’ve added get maxSpeed() . The object will now be initialized with
car1.maxSpeed = 300
car1.drive()
#=> "driving 120 mph!"
That said, we can hack around this by updating defMaxSpeed from the
outside.
car1.defMaxSpeed = 300
car1.drive()
#=> "driving 300 mph!"
The cause is that this in this.maxSpeed no longer refers to the instance itself.
//////////
// html //
//////////
<button>Drive</button>
/////////
// css //
/////////
button {
cursor: pointer;
appearance: none;
border-radius: 4px;
font-size: 1.25rem;
padding: 0.75rem 1rem;
border: 1px solid navy;
background-color: dodgerblue;
color: white;
}
////////
// js //
////////
class Car {
constructor(maxSpeed){
this.maxSpeed = maxSpeed;
}
drive(){
console.log(`driving ${this.maxSpeed} mph!`)
}
honk(){
console.log(`honk!!!`)
}
}
const car1 = new Car(120)
Of the 3 different ways to trigger drive() on a button click, only the last
feels straightforward.
$('button').click(car1.drive)
$('button').click(car1.drive.bind(car1))
$('button').click(_ => car1.drive())
My original thought was to use factories to skip “this” entirely, but it turns
out there is an easy way to solve the scope issue with “this” and classes.
class Car {
constructor(maxSpeed){
this.maxSpeed = maxSpeed;
}
drive = () => {
console.log(`driving ${this.maxSpeed} mph!`)
}
}
Notice how the problem was solved by swapping drive(){...} with drive =
$(‘button’).click(car1.drive) .
return {
drive: () => console.log(`driving ${maxSpeed} mph!`),
}
}
Here the factory returns an object with a function inside it. Every object will
have that same duplicated function inside it.
That said, I’m skeptical how often we need to generate 10k of the same
object in a browser during day-to-day development.
Classes follow the object oriented programming paradigm and are literally
templates for objects.
Another argument I’ve already alluded to is that all “problems” with classes
are simply the way the language was intended to work.
Conclusion
As someone new-ish to JavaScript, I do find factories simpler and less prone
to side effects.
If you disagree or can add anything here, I’m all ears for feedback!
Updates from the world of programming, and In Plain English. Always written by our
Founder, Sunil Sandhu. Take a look.
286 11
WRITTEN BY
GreekDataGuy Follow
Getting started with How to Send CSV Files Example of tree data Creating and Integrating
TypeScript for Node With Nodemailer structure Design Systems with
Bart Wijnants Zubair Ahmed in The Startup Ahmed Yagoub
StencilJS
Giancarlo Buomprisco in The
Startup
How To Build Your First Building a React Native Signal-cli using javascript HACKBACK Write-up
App With Electron Chat App — Part One: Pavol Travnik Shubham Ingle
Carol-Theodor Pelu in
Basic Messaging |
BucharestJS
PubNub
Krissanawat Kaewsanmuang