JavaScript Factory Functions in 1 Minute #shorts

preview_player
Показать описание

What is factory function in JavaScript?

JavaScript Factory Functions from the series "10 Things JS Developers Should Know"

This is episode 3 in a 10 part series I’m calling 10 things javascript developers should know but probably don’t. In this series, we’re gonna cover some of the most fundamental and important parts of JavaScript that most of us don’t actually know, and barely touch. Today, for episode 3, we’re gonna explore Factory Functions, the idea behind them, the problem they solve and a few practical examples. Episode 3: Factory Function. Let’s go!

All the code from this series is available here:
Рекомендации по теме
Комментарии
Автор

Factory pattern is one of those design patterns that are cross programming language concepts. Just like dependency injection and other related concepts.
Actually more suited to real object oriented languages. But as you see, in JS, it's possible as well, but a bit less of a huge thing.
It's all in order to find ways of creating objects in a more flexible way, composition over inheritance. Getting rid of older, less flexible design forms that eventually end up in hard-to-modify code.

jongeduard
Автор

Isn't that what a class is for? How do they differ?

deemon
Автор

I use this a lot as a "semantic wrapper" for ad-hoc objects that only has properties. However when it gets more involved, e.g. lots of methods, I migrate to a full class. I could use prototypes via `Object.assign()` (which IMO is a very cool function) but the Intellisense for that sucks in VSCode

(TypeScript really, since the Intellisense is based on TS, and TS more or less enforces the class approach with typing and stuff)

re.liable
Автор

when u need an unnecessary name for a regular function)))

razor-eg
Автор

writing dynamic functions should be the goal of almost any function

surbaale
Автор

I'd really prefer if the "append" was not there, because it makes the "function" impure.

furo.v
Автор

Abstract factory sends you "Hello")

ghostsvsghost
Автор

I use factories in different languages to help me test my code

SpaghettiRealm
Автор

I made one that makes wdio getters for me
It's fukkin sweet

mrscsi
Автор

in my experience in almost all cases where it’s getting more complicated it’s better to use a class and a constructor.

just_noXi
Автор

because with THE GENERIC INTERFACE you avoid prepeating the generating manually

chauvoluuhuong
Автор

TIL ive been doing this for years without knowing the name for it

abdulamite
Автор

wasnt this the old way of writing classes?

thatboisans
Автор

Can the custom hooks in react be considered kind of factory functions???

syedhaider
Автор

Can't we just make costume function that returns an object and with keys and values depending on variables?

willb.r
Автор

In this case can I just use react components, json(stored with data) and use map functions to render all of them?

balluvwdwadi
Автор

The fact that the example function both appends the element to the DOM and then returns it is giving me anxiety.

MrMudbill
Автор

I think he wants to say HOC but says it as factory function

anupthakurwar
Автор

I hate class, factory fun all day every day

kasper
Автор

What does this offer than using a regular function?

nickelion