text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
Yep.
4,189.279
0.5
So repeating for the camera, line 25, thisIsHoisted is declared as a constant
4,189.779
4.921
so it cannot be changed, whereas line 21 is declared as a function and so it can
4,194.7
5.13
be changed, which is absolutely correct.
4,199.83
2.59
What happens if I try to do this up here?
4,202.42
1.98
Yeah, so we're going to get an error.
4,212.74
4.38
thisIsHoisted is not defined, but why?
4,217.12
4.2
Because as we talked about earlier, these things
4,221.32
2.19
called consts are not available for use until they're actually declared.
4,223.51
4.74
So certain things are hoisted.
4,228.25
2.11
Var.
4,230.36
0.65
So the declaration of these variables are hoisted.
4,231.01
2.7
Function definitions if they're declared like this are hoisted.
4,233.71
3.39
But if we create what's called an anonymous function,
4,237.1
2.28
a function without any name, and set that equal to--
4,239.38
4.89
or assign that to be a constant, then that constant is not created.
4,244.27
7.08
Does that make sense?
4,251.35
3.25
So what happens if I try to do this?
4,254.6
3.69
Yeah, so same thing.
4,258.29
1.985
It errors.
4,260.275
3.06
What happens if I try to do this?
4,263.335
1.375
What's hoisted?
4,268.165
0.625
So notice-- so notice the difference in the two errors.
4,279.038
12.062
They're actually not the exact same error.
4,291.1
3.07
So who wants to give a guess at what's going on here?
4,294.17
4.45
So when I declared it with a let that's saying reference error,
4,298.62
3.53
thisIsHoisted is not defined, whereas when I use a var,
4,302.15
3.22
it says TypeError, thisIsHoisted is not a function.
4,305.37
3.871
Why might that be?
4,309.241
0.749
Yeah?
4,309.99
0.5
Uh huh.
4,317.07
0.99
Exactly.
4,324.02
1.266
So repeating for the camera., and I'll bring up the code as well.
4,325.286
2.708
So down here, so the first time when we declared this with the let,
4,333.91
4.77
it's not declared at all.
4,338.68
2.47
This variable does not exist at all.
4,341.15
1.98
So this is not hoisted, the JavaScript does not
4,343.13
2.81
know what that means on line 1.
4,345.94
2.53
But when I use a var here, remember, it hoists
4,348.47
3.26
the declaration of this variable.
4,351.73
1.44
So it creates a variable called thisIsHoisted.
4,353.17
2.58
However, it does not assign it a value until line 25 is executed.
4,355.75
4.05
And so at line 1, thisIsHoisted exists.
4,359.8
3.33
It's just equal to undefined.
4,363.13
1.87
And if I try to invoke it like a function,
4,365
2.2
it says, hey, this is an undefined variable,
4,367.2
2.62
I can't invoke it like a function, this is a TypeError.
4,369.82
3.21
And so even though both of these things errored, the reason that they errored
4,373.03
4.11
is slightly different.
4,377.14
1.11
In case one when they're declared using a const or a let,
4,378.25
4.04
that variable just does not exist at all.
4,382.29
2.45
However, when we declared using var, the variable exists.
4,384.74
3.83
It's just undefined, and so if we try to invoke it like a function,
4,388.57
3.18
and it says, hey, like, this is undefined, it's not a function.
4,391.75
4.08
Does that make sense to everyone?
4,395.83
2.079
So why does this happen?
4,401.29
2.84
How does this happen?
4,404.13
2.03
And the reason is actually how JavaScript is executed.
4,406.16
3.71
So there's two phases in the execution of a JavaScript file.
4,409.87
4.89
So before executing any code, it has all of the text in front of it,
4,414.76
4.47
but it hasn't executed anything, it just reads the entire file.
4,419.23
3.837
And what's it looking for?
4,423.067
1.083
Well one, it's looking for anything wrong with the file.
4,424.15
2.91
So say like my very first example when I had an array and it was missing
4,427.06
5.22
a comma, that's something that's caught in the first reading.
4,432.28
2.73
It says, hey, like, this looks like an array but it's not quite right.
4,435.01
3.66
Like, I see this thing that I'm not expecting, I'm respecting a comma here.
4,438.67
3.894
So that's one thing that's caught.
4,442.564
1.416
Other things, maybe it will add some semi-colons for you
4,443.98
3.51
and stuff like that.
4,447.49
2.67
And then any function definitions just get saved in memory.
4,450.16
4.014
It says, hey, this is a function, let's put this in memory
4,454.174
2.416
so if somebody wants to use it at line 1 they're able to.
4,456.59
4.1
Variable initializations, if they're lexically scope-- or I mean,
4,460.69
4.35
if they're lexically-scoped they will be declared,
4,465.04
2.52
but they will not be initialized.
4,467.56
1.56
Meaning anything declared with var will be
4,469.12
2.76
declared-- like there's a variable that exists,
4,471.88
2.28
but it's not going to be set equal to anything until later.
4,474.16
4.71
And then there's a second phase called the execution phase, whereby the code
4,478.87
3.57
is actually run, it's executed.
4,482.44
1.89
And so that is when things like const or let get invoked--
4,484.33
4.95
or get both declared and initialized.
4,489.28
4.14
Does that make sense to everyone?
4,493.42
2.124
Anybody have any questions on scope?
4,498.82
2.216
So why might we take advantage of this scoping?
4,505.9
3.443
Does it seem like a feature or does it seem like a bug?
4,509.343
2.291
Anybody care to guess?
4,516.96
1.314
There's no correct answer to this.
4,518.274
1.416
It's completely opinion.
4,519.69
1.38
Yeah?
4,521.07
0.5
AUDIENCE: So you've got a controller with math functions in it.
4,521.57
2.886
JORDAN: Uh huh.
4,524.456
0.962
AUDIENCE: You can call the ones there and lay down
4,525.418
2.084
a file so that you can write all your code in one giant function?
4,527.502
4.188
JORDAN: Yes So, one thing might be we can
4,531.69
2.52
have these function declarations at the very bottom
4,534.21
2.454
and then we can use it at the top.
4,536.664
1.416
And so that might be good for code organization.
4,538.08
2.88
Like somebody reading your code would know, hey,
4,540.96
2.16