Factory Function Pattern in Javascript

Kunal Singh

Sep 24, 2024

function createCar(make, model) {
return {
make,
model,
drive() {
console.log(`Driving a ${this.make} ${this.model}!`);
}
};
}

const myCar = createCar('Toyota', 'Corolla');
myCar.drive(); // Output: Driving a Toyota Corolla!
+

Check my latest Blog Post

How to Publish Your First npm Package: Creating Rainbow Highlight with Utilities

Read Now
Oh My Gawwdd!!!!!!!

Wow you have been viewing my site since 20 seconds!

+
+