text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
Like, what if you wanted them
to not point to the same thing? | 2,253.218 | 2.662 |
JORDAN: Yeah, so the
question is, what if we | 2,255.88 | 1.95 |
wanted them to have the same
value but be different references? | 2,257.83 | 3.18 |
How might we do that? | 2,261.01 | 1.26 |
There are two different ways. | 2,262.27 | 2.14 |
One, the more annoying way, would just
be to type the whole thing out again. | 2,264.41 | 4.58 |
[TYPING] | 2,268.99 | 2.916 |
So now we're guaranteed
that o and o2 are | 2,274.002 | 1.708 |
going to be different references to
an object that is basically the same. | 2,275.71 | 6.42 |
And another way would be -- | 2,282.13 | 1.6 |
there are multiple
different ways to do this. | 2,283.73 | 2.25 |
The most common way in pure
JavaScript, would be to do this-- | 2,285.98 | 3.59 |
[TYPING] | 2,289.57 | 3.486 |
Whereby, object assigned is
basically saying, hey, pass in to me | 2,297.54 | 4.315 |
a bunch of arguments. | 2,301.855 | 0.875 |
And every single argument, I'm going
to merge into the previous one, | 2,302.73 | 3.33 |
those lose keys and values. | 2,306.06 | 1.7 |
And so this is saying,
give me a brand new object. | 2,307.76 | 3.14 |
So I'm using the object literal
here to mean a new object. | 2,310.9 | 3.05 |
And then merge into it, of the keys
and values of this object called o. | 2,313.95 | 4.4 |
And so this is basically
saying, give me new object, | 2,318.35 | 3.08 |
and then set all of the keys
and values of o to be in there. | 2,321.43 | 3.92 |
So this is the way of cloning an object. | 2,325.35 | 3.6 |
But say we actually did this | 2,328.95 | 1.31 |
[TYPING] | 2,330.26 | 3.486 |
So, what do we expect
this to now print out? | 2,369.17 | 1.9 |
So, we mentioned that line nine,
we're taking the keys and values of o, | 2,374.31 | 7.38 |
and merging those into a new object. | 2,381.69 | 4.2 |
And then that line 11 we're
taking o2, getting the dot object, | 2,385.89 | 3.93 |
so accessing the value
with the key called object. | 2,389.82 | 2.94 |
And then setting that object's
key called key to new value. | 2,392.76 | 4.47 |
And then now console
logging o to object dot key. | 2,397.23 | 4.1 |
Yeah. | 2,401.33 | 1.44 |
AUDIENCE: So, new value? | 2,402.77 | 2.81 |
JORDAN: Yes, so this, so
the guess is new value, | 2,405.58 | 2 |
and that is absolutely correct. | 2,407.58 | 2.79 |
So this, so-- line nine here is
doing what's called a shallow copy. | 2,410.37 | 4.62 |
Which is just grabbing the
keys and values of some object, | 2,414.99 | 3.09 |
and just setting those blindly
into some other object. | 2,418.08 | 3.33 |
As opposed to what would
be called a deep copy. | 2,421.41 | 3.24 |
Where that would take
the keys and values. | 2,424.65 | 2.822 |
And then if the values
are objects, you'd | 2,427.472 | 1.708 |
also take those objects keys and values. | 2,429.18 | 1.666 |
Do that recursively, and basically
get every single layer deep cloned. | 2,430.846 | 5.234 |
But since object assigned just
takes the keys and values dumbly, | 2,436.08 | 4.23 |
if we have an object in there,
update that object's key. | 2,440.31 | 5.34 |
o.obj and o2.obj are still referencing
that same object in memory, | 2,445.65 | 4.38 |
so since we updated-- | 2,450.03 | 1.23 |
we mutated that object, it
would update in both o2 and o. | 2,451.26 | 5.13 |
Does that makes sense? | 2,456.39 | 3.385 |
Great, any questions about this? | 2,459.775 | 3.495 |
Yeah. | 2,463.27 | 0.5 |
AUDIENCE: How would you do a deep copy? | 2,463.77 | 1.74 |
JORDAN: So, how would you do deep copy? | 2,465.51 | 2.16 |
That's a great question. | 2,467.67 | 1 |
There are multiple different ways. | 2,468.67 | 2.91 |
So most people would say use a library. | 2,471.58 | 3.32 |
Meaning, rather than implementing
this thing on your own, | 2,474.9 | 2.85 |
just take somebody
else's implementation. | 2,477.75 | 3.63 |
But lets, let's actually do that. | 2,481.38 | 1.56 |
That's a good question. | 2,482.94 | 1.06 |
So, how would we do a deep copy? | 2,484 | 2.116 |
[TYPING] | 2,486.116 | 3.332 |
So, I know we haven't talked
a ton about JavaScript yet, | 2,490.88 | 3 |
but lets actually tried
to do this together. | 2,493.88 | 2.35 |
So, let's call a function, deep copy. | 2,496.23 | 4.217 |
And we're going to pass
into it some object. | 2,500.447 | 1.833 |
And how would we implement
this, if we're guaranteed | 2,506.17 | 3.1 |
that no objects have values of objects? | 2,509.27 | 4.5 |
Meaning we are guaranteed not
to have objects within objects. | 2,513.77 | 4.435 |
How might we do this? | 2,518.205 | 0.875 |
AUDIENCE: Check for the type of the key. | 2,519.08 | 2.42 |
JORDAN: Yes, so we can check
for the type of every key. | 2,521.5 | 2.291 |
But if we're guaranteed that no
values are going to be objects, | 2,523.791 | 2.939 |
we can just do a shallow copy right? | 2,526.73 | 3.81 |
Yeah. | 2,530.54 | 1.631 |
AUDIENCE: So check every value? | 2,532.171 | 1.491 |
And if it's, the value is [INAUDIBLE]
we have to recursively deep copy that. | 2,533.662 | 5.478 |
JORDAN: Yeah. | 2,539.14 | 0.58 |
So hold that thought. | 2,539.72 | 0.874 |
Let's actually implement
this as if we know that there | 2,540.594 | 3.596 |
are no objects inside of objects. | 2,544.19 | 2.14 |
So if that were true, we could
just return the shallow copy right? | 2,546.33 | 3.22 |
So object.assign | 2,549.55 | 2.515 |
[TYPING] | 2,552.065 | 3.123 |
So this would be a perfectly
valid implementation, | 2,557.527 | 2.083 |
if we knew that there's no such
thing as objects within objects. | 2,559.61 | 4.08 |
But since there are, we're going
to have to do some magic here. | 2,563.69 | 4.66 |
So, can you repeat your
recommendation again? | 2,568.35 | 4.116 |
AUDIENCE: So, check if the-- | 2,572.466 | 3.992 |
any of the values is an object. | 2,576.458 | 3.992 |
Then you can't just take
that value into a new object, | 2,580.45 | 5.489 |
just to reference the same object,
we need to deep copy that object, | 2,585.939 | 7.984 |
instead of just taking [INAUDIBLE]. | 2,593.923 | 3.992 |
JORDAN: So basically, so
check every single value, | 2,612.96 | 3.537 |
and see if it's an object. | 2,616.497 | 1.083 |
If it is an object, then go
ahead and deep copy that object. | 2,617.58 | 3.3 |
Otherwise, return that value. | 2,620.88 | 3.206 |
Cool. | 2,624.086 | 1.316 |
So let's do that. | 2,625.402 | 0.708 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.