Press "Enter" to skip to content

Factory Functions in JavaScript


hello function years today we are

talking about factories factories are

simply functions that create objects and

return them in most cases you can use

factories instead of classes and

factories are much simpler and less

convoluted than classes are this is a

weekly show where we try to become more

confident and excited about programming

by learning intriguing things that we

didn’t know before

pre-requisite in order to understand

this video you need to be familiar with

closures if you’re not completely sure

what closures are you should watch the

previous episode on that topic first

before I show you a factory function I

want to show you a class this is a dog

class it does some setup in the

constructor assigns a property to itself

called sound the class also has a method

talk which uses that sound

we then instantiate at all say foods

which we then call talk on and sniffles

promptly tells us what this is fine but

there are a couple of annoying things

with classes if we assign the top method

to something like click handler here

things will break because now the this

keyword inside the top method here will

not be sniffles this is going to refer

to something else in this case I think

it’s going to be the Dom element or

something that it’s not important

exactly what but what is important is

that it isn’t going to refer to sniffles

like you would intuitively expect to

work around this you can use bind to

force this to be sniffles this is a very

common thing to do but it hurts my

gentle and beautiful soul to look at

this code it looks like cold blue

another way of dealing with this problem

is to wrap the top call in a function

like this that looks better

but it annoys me that I have to remember

to do this at all to me it feels like a

stupid trap in a shitty computer game in

fact I think that the concept of this

and new in JavaScript feels like that in

general

new and this are some kind of

unintuitive weird clown rainbow trap

that you just trip over all the time and

I am so ashamed of it every time I

introduce people to JavaScript but there

is hope because in JavaScript you don’t

have to do classes in Java or C sharp

and some other languages you have to use

classes that’s the way of instantiating

objects but in JavaScript you have a bit

more freedom so maybe we can try some

other way enter factory functions here

is our dog re-written as a factory

function now dog is simply a function in

it we create a variable sum and we

assign the value woof to it we then

return a simple object literal with one

property Tork a function which logs out

the value of sum and because of the

awesomeness of closures top has access

to the variable sound a bonus feature

here is that the code outside of the Dom

doesn’t have access to the sound

variable so the sound variable is

actually properly private to the dog

which it isn’t in the class I notice

here that we are not using that this

keyword at all and because of that the

reference to the sound variable will

always be correct even when we use class

in the way that broke in the class

example like this I think that for

almost all cases you will be better off

using factories for other than classes

because your code will be simpler and

easier to reason about there is one case

where I think it’s not like to use

classes and that is if you find yourself

in a situation where you need to create

a lot of objects it

cause classes have a bit better

performance but note here when I say a

lot I mean a lot instantiating an object

on my computer using a factory takes

zero point zero point zero point zero

point four milliseconds using a class is

twice as fast actually 0.0.0.0 hmm point

two milliseconds if you do the math this

means that if you’re creating say 10,000

instances using classes over factories

will earn you a directed a to

milliseconds so if you’re creating just

a couple of hundred or even a couple of

thousand items per frame the performance

difference between a factory and class

is not going to matter to you if you are

creating more than 10,000 instances per

frame then classes might be for you but

you should really ask yourself if the

problem isn’t that you’re creating

10,000 items per frame in the first

place

in summary factories or simply functions

that create and return objects we looked

at a simple example of how you can use a

factory instead of a class and how the

factory is less convoluted than a class

we also talked about how classes are a

bit faster but that it is by so little

that it only matters if you’re creating

at ten thousand items per frame or more

do you have thoughts or questions maybe

you even disagree good post a youtube

comment down below and me or a fellow

viewers I’ll respond to your inquiry

again in this show we try to become more

excited and confidence about programming

I’m learning intriguing things that we

didn’t know before new episodes every

Monday morning so make sure that you

don’t miss the next episode subscribe to

the channel here on YouTube or follow me

on Twitter at mvj week until next Monday

stay curious you

Please follow and like us: