Press "Enter" to skip to content

Object.create – Object Creation in JavaScript P6 – FunFunFunction #57


today we are exploring object dot create

what it is why it exists and how to use

it if you are new to the channel welcome

I am your host npj and you are watching

fun fun function first of all what is

object dot create object create is a

static method on the object prototype

that creates a new object with the

prototype set to a certain object but

that’s a very long explanation and even

though I wrote it and I read it I got

bored half way and so now let’s instead

have a look at what it does let’s say

that we are writing an enterprise-grade

application and we have a normal object

that exists in such applications such as

let’s run that I know I’m calling this

file object trait sample j s and it says

new you’re aware that cat is sick that

is fine but we bought multiple types of

cats in our application that sounds

alright I’m gonna run that no the dart

object sample dodge it and you’ve I’m

let’s run that no dot object example

cats cats cats cats let’s walk through

this so we’re creating a simple object

here this is nothing special or

wonderful about this object it’s just an

object mineral with a with a method or a

property that we set to a function which

is fights then we can call it a method

and the console dot log it logs out the

properties sound on whatever this is

down here we call make sound so that

means it will go into this and it’s

gonna come to this line here

and it’s going to pick out whatever

sound is and we have set sound here on

this line we’ll set in the sound of mark

to move and that is why it logs out

sound if the concept of this confuses

you you might want to check out the

previous episodes in this series on

object creation in JavaScript you can

find a playlist with all the episodes in

in the description of this video so

check that out

if you watch the series you should be

familiar with everything up to this

point what you don’t know is what this

does what is this what is happening

inside of object of great what kind of

sorcery does this do object or create

creates a new object with the prototype

set to certain object so this object out

create will create a completely new

fresh object and it will set the

prototype of that object to be cat let’s

verify that I’m going to bright console

dot log is

smart a cat then I’m going to do this

I’m going to call Mark don’t is

prototype of cat I’m gonna run that node

dot object RIT Sambo J s force I did not

expect that

oh okay it’s because I’m I’m thinking in

backwards land so this does not make

sense I’m asking if Mark is a prototype

of the cat which it mark isn’t it’s the

cat that is the prototype of mark mark

is being derived or copied or whatever

you like to however you like to think

from the cat so it should be cat is a

prototype of of Mark is cat the

prototype of Mark and now I run that

again and you see that is Mark a cat

well true I accidentally said there

before that mark and and waffles or

copies of the cat which is not not true

that’s an incorrect way of thinking

about it what waffles and Mark are is a

new objects that don’t have the property

make sound themselves but they have a

reference to a prototype a cat and

whenever we ask for something like make

sound that does not exist on waffles and

mark themselves they delegate that call

to their prototype and ask the prototype

do you have the method make sound and if

so call that in a little bit we’re going

to look at how to use object.create but

first I want to talk about why object or

create exists in the first place what’s

the purpose of this thing well object

object is more natural to the prototype

model than the new keyword so in

previous episodes we looked at

how to apply the new keyword to a

function in order to simulate something

that looked sort of like classes and

that way was for a long time there the

factor or the the standard way of

creating objects in JavaScript it was a

kind of sort of class the problem was

that it it kind of looked like a class

but it looked really weird and it had a

bunch of quirks that just didn’t really

make sense from the standpoint of what

you expected of a class so Douglas

Crockford which you might know for being

the author of JavaScript good parts he

also did jslint you can think of Douglas

Crockford as the grumpy grandfather of

JavaScript and we was annoyed with the

state of object creation in JavaScript

so he wrote object creates as an attempt

to make something that was more in line

with how the prototype worked like

something that made a little more sense

and didn’t try to pretend being

something that it’s not you know a class

and Uggla’s Crockford being a person

that has a significant influence on the

JavaScript community managed to get

object create into the language itself

so object or create is built into

JavaScript even though it wasn’t at

first the more inquisitive student might

notice that we have been setting the

prototype directly in earlier episodes

we have us created objects and then to

stop just setting the prototype why why

introduce object create why doesn’t that

just make things more complicated and

yes it does object or create jumbles

together the creation and the of the

object and setting of the prototype but

a set prototype of and

messing with the prototype directly on

existing objects is a very bad idea from

a performance standpoint it makes it

very hard for the JavaScript engine to

optimize the speed of your application

which defeats the purpose of using the

prototype in the first place so in real

life we never use set prototype Alban we

never assign new prototypes to object

like we did in previous episodes set

prototype office very nice when

explaining the concept of a prototype

but in a real life application you

should not be using it you should be

using object or create which brings us

to how to use object.create but before

we do I’m going to take a walk and a

in the last week I’ve gotten a bit

addicted to the video Q&A app wale I’ve

been answering a lot of questions away

up this morning I answered this one by

Tong Qi how do you handle ideas about

the things that you can solve with your

programming skills and what stops you

from turning said solution into a

business man this question hits close to

home

for me this is a huge problem for me I

have like a million ideas all the time

and being a programmer you see that oh I

could solve this problem with software

so easily and it would benefit so many

people if if there just was a piece of

software that’s all this ah and this

line of thinking has caused me to start

each Li 30 projects but I stopped myself

from doing that nowadays because those

projects never amount to anything

the reason these projects are almost

never amount to anything

is that launching a software product is

not just coding it it is building a

business around it creating business

model investing in that business

launching that product marketing that

product and then over time maintaining

and and running that business building

the software is a tiny tiny bit of all

that work so now whenever I think of

this awesome software idea that would

change the life of a couple people and

it would be real great I asked myself

hmm am I really willing to spend the

next one or two years of my life

focusing on focusing on this thing

because that is honestly what it would

take to

to make it happen and usually not is not

a case it’s the ideas are often not that

way I really don’t like when people say

that oh I have so little time time is

the only thing that we have in abundance

like it’s this huge resource of of ours

that gets replenished every single day

so if you have that feeling that oh

there’s so little time and so busy then

the problem is not really the amount of

time that you have you have vast amounts

of it the problem is how you spend the

time in case you’re new to this channel

I live in Sweden and I’m also

stereotypically close to IKEA or Kia

beset in Swedish and this is where I’m

going to eat lunch because it’s lunch is

all right we’re back how to use

object.create we’re back to the example

that we had before I’m going to clear

the console a great way to understand

tools you know libraries or built-in

functions is to use try to really

implement them so that is what we’re

going to do first we’re going to create

object or create them we’re gonna call

okay so what arguments does our object

rate function take well it takes a

prototype let’s just call that proto

because I think that a prototype is

probably a reserved keyword I don’t know

but I’m guessing object.create

actually takes a second object second

argument that is a properties object we

are not going to deal with that today

it’s not really related to how object

treats works and you don’t need it to

understand this and I honestly don’t

think you need it period the first thing

that object not create does internally

is to create an object here’s a simple

object and then it’s going to set the

prototype of that object we use the set

prototype of and we’re going to set the

prototype of the object to our proto and

then we’re going to return the object

let us run that node object for example

ah keep forgetting the test

yes and it still gives the same output

as before so if we take a look here at

object create like what this does so

check this out

create a new object here create a new

object and it then sets the prototype of

that object to a certain object which in

this case is is is proto that’s it

that’s the gist of what object create

dots but we’re not going to use the set

prototype all because that is a horrible

from a performance standpoint and

performance is again why we’re using the

prototype in the first place

so we’re going to do object create here

yes

when using object or create in real life

you like this seems this this

implementation might seem wrong to a lot

of people and that is because it is if

this was a class there

in initialization here this would

probably be part of some constructor if

this was a real object there would

probably be a lot more functionality and

setup that goes on here

probably some conditionals and some

checks for that you’ve set all the cat

Anatomy so where do you put constructor

logic because object doc object create

there is no space for a constructor and

the most common pattern is to simply

just use an init function like init

letter that the third function and just

like a constructor you would here

provide the data that is needed to

construct the cat for instance sound and

then you’d go set this don’t sound

equals sound I know this is a contrived

example you have to imagine that the

init function actually contains more

logic I forgot a comma there and now

we’ll replace this sound assignment here

with in its name

num there and the same thing down here

in it fruit rook we’ve run that again to

make sure that it works as before yes we

get the same output a Halle pattern here

is to just return this because then you

can chain these calls and do object

traded cat maybe a delete here the law

do the same thing here I’ll just bunk

bunk bunk a new line and I run that

again same output and that is it object

not create is a very simple

straightforward way to create objects

that use prototypes object or create

it’s nice it has great CPU and memory

characteristics and as long as you

understand how what the prototype is

this is a very straightforward

nice pattern if anything was confusing

please post a question down in the

comments if it wasn’t confusing please

help out the person that has posted that

question in the comments you have just

watched an episode of fun fun function

if you’re new welcome this is a

programming show and I post new episodes

every Monday morning Oh 800 GMT you have

just watched an episode that was a bit

tutorial like but I also do these other

musings episodes where I’m there are

more thought pieces where I talk about

architecture working in a team

productivity and stuff like that you can

check that out in this playlist

I am mvj until next Monday morning stay curious

Please follow and like us: