text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
And if you have an array of like 100 different things,
3,347.35
2.93
all hundreds of those, deep copying every single prototype gets pretty big.
3,350.28
3.9
And so what is the danger of storing a reference to the prototype,
3,356.75
3.19
rather than copying the whole thing?
3,359.94
5.605
AUDIENCE: I guess if change it, [INAUDIBLE]..
3,365.545
3.985
JORDAN: Yeah exactly.
3,369.53
1.11
If you change it, then it changes for every single value of that type.
3,370.64
3.66
So let's do that really quick.
3,376.83
1.52
So, so say we have, so we solved num, which is equal to 42, right?
3,378.35
7.93
And if you do num.toString, what so we expect to come out?
3,386.28
4.49
42 is a string right?
3,390.77
1.67
But say, some devious programmer was doing this thing
3,392.44
2.807
where he did number.prototype.toString
3,395.247
1.583
[TYPING]
3,396.83
2.644
And you actually override that to be some function that will return 100.
3,402.7
10
Now what happens if I call a num.toString?
3,412.7
3.36
Wait a second.
3,416.06
1.03
[TYPING]
3,417.09
1.907
So that could have some dangerous penalties right?
3,418.997
2.083
So if I were to change the prototype of the number class,
3,421.08
3.67
even though num was declared 100 lines prior to be the number 42.
3,424.75
6.2
And we tried num.toString here and it returned 42.
3,430.95
3.81
If we were to change the prototype later,
3,434.76
2.1
it affects everything that has ever happened.
3,436.86
2.97
So num.toString now starts returning 100.
3,439.83
2.37
And everything that will happen.
3,442.2
1.36
So if I were to do--
3,443.56
0.833
[TYPING]
3,444.393
3.288
Everything in the future also has those consequences.
3,452.04
2.86
So changing the prototype is something that is very dangerous,
3,454.9
2.99
and is recommended against doing.
3,457.89
3.032
Does that make sense?
3,460.922
1.488
Cool.
3,466.374
0.5
So let's actually take a short break.
3,472.83
3.1
All right welcome back.
3,475.93
1.78
So let's talk about scope now.
3,477.71
3.47
So what the heck is scope?
3,481.18
1.2
So Scope is a term that's talking about variable lifetime,
3,482.38
3.42
and how long these variables actually exist.
3,485.8
1.875
And so there are a couple different types of scoping.
3,490.162
2.208
There's lexical scoping, which is that keyword var that you might
3,492.37
2.85
see if you're reading old JavaScript.
3,495.22
2.84
And there's block scoping, which refers to how things like const or let
3,498.06
5.23
are scoped.
3,503.29
1.05
So lexical scoping is basically saying, hey, give me a variable
3,504.34
4.27
and it will exist for as long as since it
3,508.61
2.27
was declared all the way until the end of a function ends or the file
3,510.88
4.8
if it's in a file.
3,515.68
1.5
Whereas the analog would be block scoping
3,517.18
1.92
where it behaves a lot like something behaves in C.
3,519.1
3.62
Where basically a variable will be around
3,522.72
3.19
from when it's declared until the next end curly brace is reached.
3,525.91
4.6
And so that there is the big difference between var and const and let.
3,530.51
3.65
And the difference between const and let is
3,534.16
2.01
that const is something that can't be updated,
3,536.17
2.7
meaning if I set a variable to be a constant, it
3,538.87
2.82
means I'm not going to update that reference later,
3,541.69
3.51
whereas let can be updated.
3,545.2
2.01
So if I were to do const thisIsAConst and set about equal to something
3,547.21
6.81
like 50, if I were to say thisIsAConst and try to update that to be 51,
3,554.02
8.79
I'm going to get an error actually that says, hey, you call that a const?
3,562.81
4.49
But you're trying to change it.
3,567.3
1.49
That's not OK.
3,568.79
1.83
And so no matter how I want to change it--
3,570.62
1.75
say I do that plus-plus or something, that's also going to fail.
3,572.37
3.74
Whereas if I did something like let thisIsALet equal to 50,
3,576.11
7.26
I can go ahead-- or 51, I made a typo, but I can just say,
3,583.37
4.32
hey, it was a typo, thisIsALet, let me change that to 50 and we're all good.
3,587.69
7.36
I mean, I can also do that plus-plus or change it however I want
3,595.05
3.12
and it will actually update that.
3,598.17
6.16
Note that I said the reference can't be updated.
3,604.33
4.434
I did not say anything about things being immutable.
3,608.764
2.166
So if I get const obj equals this empty object, if I then--
3,610.93
9.71
I can't update it to be--
3,620.64
2.19
point to a different object.
3,622.83
1.23
If I try to do obj is something else, I'm going to say,
3,624.06
4.32
hey, you call this a const and you're trying to change it.
3,628.38
3.78
But I can still do obj.a and set that equal to a, because why?
3,632.16
6.39
Anybody care to tell me why?
3,638.55
1.65
Exactly.
3,646.95
0.57
So the pointer is still pointing to the same object.
3,647.52
2.43
The reference has not changed.
3,649.95
2.83
So we mutated that object, but it's still
3,652.78
2.577
pointing to that same place in memory.
3,655.357
1.583
It's still pointing to the object that exists over here
3,656.94
3.26
and that reference has not been changed.
3,660.2
2.44
Does that distinction make sense to people?
3,662.64
2.374
It's a pretty important one.
3,665.014
1.166
Cool So let's play a little bit with these variable lifetimes.
3,669.13
4.953
So I said before that if I tried to do something like this, what happens here?
3,689.88
12.04
Error.
3,706.275
0.5
Why?
3,709.54
0.5
Because it's a constant and we can't update a constant.
3,710.04
3.2
And we can confirm this.
3,713.24
2.718
Oh.
3,715.958
1.104
It'll actually tell us, hey, a TypeError.
3,717.062
1.708
You call this a constant variable but you're trying assign it.
3,718.77
2.37
That's not OK.
3,721.14
0.583
But if we did--
3,728.51
0.82
this is OK because the reference to that object did not change,
3,741.05
3.58