text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
if I'm looking for these functions, they're
4,543.12
2.01
all going to be declared together at the bottom.
4,545.13
2.22
And they might be used everywhere, but all the function declarations
4,547.35
3.06
are all going to be here.
4,550.41
1.041
So that might be a feature because it's good for organization.
4,551.451
4.209
So say we're to play devil's advocate.
4,555.66
2.91
Who might see this as a bug?
4,558.57
2.346
Uh huh.
4,574.038
3.162
OK.
4,577.2
0.5
So the question is, like, why can we declare two variables
4,577.7
2.872
with the same name when they look like they're in the same scope,
4,580.572
2.708
specifically with this var keyword?
4,583.28
2.82
Which is another thing where it's a bug/feature that a lot of people use.
4,586.1
5.66
And so if JavaScript were to be updated and that bug/feature were to disappear,
4,591.76
4.99
a lot of code would break.
4,596.75
2.52
So a lot of people have took advantage of this.
4,599.27
1.98
And basically it's the same thing as like, why is typeof a null object?
4,603.862
2.958
Just because it is.
4,609.77
1.08
And we can't change that because people rely on that behavior.
4,610.85
4.39
I know it's kind of an anti-answer, but does that kind of makes sense?
4,615.24
3.86
Yeah, you should-- so there's not really a good reason to use var anymore.
4,622.34
4.99
With ES6, everything supports const and let.
4,627.33
3.97
And so I've been using them in all of the examples except for these.
4,631.3
5.065
And I think you should definitely use them as well.
4,636.365
2.125
The reason I'm getting var is one, so if you see this,
4,638.49
3.68
you know what's kind of going on, and because a lot of legacy code-- a lot
4,642.17
3.54
of code written two, five years ago, 10 years ago
4,645.71
3.18
uses var just because it was the only option.
4,648.89
1.92
One thing that I didn't mention earlier is that you can also
4,653.76
3.612
declare a variable like this.
4,657.372
1.208
So this is another way of declaring a variable.
4,668.47
4.08
This creates a global variable.
4,672.55
2.517
It's something that you probably won't see that often, there's
4,675.067
2.583
really no reason to do it.
4,677.65
1.77
But in case you ever see that, that's what it is.
4,679.42
4.27
So if you declare a variable without giving it a keyword like let, const,
4,683.69
3.555
or var, it creates it globally.
4,687.245
2.18
But there's no reason for you to use this, really.
4,692.23
3.21
It's just if you ever see it, that's what it is.
4,695.44
2.398
Any questions at all about scoping?
4,701.19
2.38
It's a pretty important concept.
4,703.57
3.95
And for online folks as well, remember, you can post in Slack
4,707.52
3.18
and the staff will either field those questions or pass them to me.
4,710.7
4.846
Cool So let's move on to our final topic of the day.
4,719.21
2.72
So the global object.
4,726.37
2.11
So the way these things all work is basically in any given runtime,
4,728.48
7.46
there's this thing called a global object.
4,735.94
2.65
And so all variables, all functions are actually
4,738.59
2.63
keys or parameters or methods on the global object.
4,741.22
3.9
So in browser, there's this thing called a window
4,745.12
3.18
which is a global object of a window.
4,748.3
1.75
So this is a browser environment.
4,750.05
5.78
And if I type window, I see this thing.
4,755.83
3.92
And it has a lot in it.
4,759.75
4.13
But basically the window is this global object,
4,777.01
2.27
and on it there is all sorts of stuff.
4,779.28
3.01
So we see $0, $0a.
4,782.29
3.17
So these-- so any variable that you declare in this console
4,785.46
4.47
gets put onto this global object called window.
4,789.93
3.96
And so actually, let me open a brand new browser tab.
4,793.89
2.83
And we'll get a little bit of a cleaner window.
4,803.212
1.958
So the reason that we saw these things called $0, $whatever,
4,805.17
3.13
is because I open these dev tools on the same tab as my lecture slides.
4,808.3
7.03
And the way that Google Slides works is since it's
4,815.33
5.79
something dynamic on the web, it has to be using JavaScript.
4,821.12
3.09
And since it's using JavaScript, obviously it's
4,824.21
2.01
creating a bunch of functions, it's creating a bunch of variables.
4,826.22
2.75
And all of these things end up on the window object.
4,828.97
2.32
And so when we check out-- when we inspected the window
4,831.29
2.7
object of that particular tab, there was a bunch of stuff
4,833.99
2.79
on it because that's how Google Slides is working.
4,836.78
3.72
And when we created a new tab, this is a brand new tab
4,840.5
6.66
and therefore no JavaScript has been executed,
4,847.16
2.08
so there's a lot fewer variables and stuff on this new tab.
4,849.24
5.7
And so whenever we create new variables-- so
4,854.94
2.09
say we do const x equals this is a new variable, we see x is this.
4,857.03
7.98
We can also do this window.x--
4,865.01
3.76
I declare it as a const.
4,868.77
1.5
So the way that browser windows handle these blocks of variables
4,879.24
2.71
is a little bit different.
4,881.95
1.91
So we'll use var for the sake of explanation.
4,883.86
2.92
So when I create this new variable called var y,
4,886.78
3.65
it is the exact same thing as window.y And so if we inspect the window,
4,890.43
9.03
we see a bunch of things that are part of the JavaScript API, the browser
4,899.46
3.38
API, all of these things.
4,902.84
2.23
And if we go all the way to the very bottom,
4,905.07
7.03
so this is stuff that is created in the new tab.
4,912.1
5.258
L, M, N, O, P, Q, R, S. We see this y here.
4,917.358
5.162
So part of that window object is that variable called y
4,922.52
3.41
that we stuck on the window.
4,925.93
1.55
And so even though we declared it with var y equals something,
4,927.48
3.31
it ended up on that global object, and that's
4,930.79
2.19
how JavaScript keeps track of all of your valuables and stuff.
4,932.98
4.98
And so what happens when we're in the node environment and we type window?
4,937.96
7.64
up window's not defined.
4,945.6
1.692
And it's because in the node environment,
4,947.292
1.708
the global variable is not called window, it's actually called global.
4,949
3.13
And if we type global, we now see all of these things.
4,952.13
5
And a lot of this will overlap with that window object in the browser,
4,959.87
3.76
but since the browser API has things in it that is not necessarily
4,963.63
4