When They "feel" the Same - Code - Part 5

What’s the Difference Between Scope and Context in JavaScript? When Scope and Context "feel" like the same thing (but they are not)
2 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€85.55
List Price:  €119.77
You save:  €34.22
£74.16
List Price:  £103.84
You save:  £29.67
CA$137.94
List Price:  CA$193.12
You save:  CA$55.18
A$152.78
List Price:  A$213.90
You save:  A$61.12
S$128.67
List Price:  S$180.14
You save:  S$51.47
HK$780.19
List Price:  HK$1,092.31
You save:  HK$312.11
CHF 80.30
List Price:  CHF 112.42
You save:  CHF 32.12
NOK kr1,004.34
List Price:  NOK kr1,406.11
You save:  NOK kr401.77
DKK kr638.63
List Price:  DKK kr894.11
You save:  DKK kr255.47
NZ$170.14
List Price:  NZ$238.20
You save:  NZ$68.06
د.إ367.21
List Price:  د.إ514.11
You save:  د.إ146.90
৳12,172.20
List Price:  ৳17,041.56
You save:  ৳4,869.36
₹8,825.07
List Price:  ₹12,355.46
You save:  ₹3,530.38
RM422.33
List Price:  RM591.28
You save:  RM168.95
₦152,828.71
List Price:  ₦213,966.31
You save:  ₦61,137.60
₨28,385.35
List Price:  ₨39,740.62
You save:  ₨11,355.27
฿3,218.97
List Price:  ฿4,506.69
You save:  ฿1,287.72
₺4,125.04
List Price:  ₺5,775.22
You save:  ₺1,650.18
B$544.72
List Price:  B$762.63
You save:  B$217.91
R1,768.44
List Price:  R2,475.88
You save:  R707.44
Лв167.48
List Price:  Лв234.49
You save:  Лв67
₩139,005.46
List Price:  ₩194,613.20
You save:  ₩55,607.74
₪333.96
List Price:  ₪467.56
You save:  ₪133.59
₱5,689.88
List Price:  ₱7,966.06
You save:  ₱2,276.18
¥14,815.95
List Price:  ¥20,742.93
You save:  ¥5,926.97
MX$1,867.49
List Price:  MX$2,614.56
You save:  MX$747.07
QR364.04
List Price:  QR509.67
You save:  QR145.63
P1,439.16
List Price:  P2,014.89
You save:  P575.72
KSh12,918.70
List Price:  KSh18,086.70
You save:  KSh5,168
E£4,854.53
List Price:  E£6,796.54
You save:  E£1,942
ብር14,306.63
List Price:  ብር20,029.86
You save:  ብር5,723.22
Kz91,186.39
List Price:  Kz127,664.59
You save:  Kz36,478.20
CLP$97,214.27
List Price:  CLP$136,103.87
You save:  CLP$38,889.60
CN¥713
List Price:  CN¥998.24
You save:  CN¥285.23
RD$6,340.36
List Price:  RD$8,876.76
You save:  RD$2,536.40
DA12,985.04
List Price:  DA18,179.58
You save:  DA5,194.53
FJ$225.61
List Price:  FJ$315.87
You save:  FJ$90.25
Q767.35
List Price:  Q1,074.33
You save:  Q306.97
GY$20,923.93
List Price:  GY$29,294.34
You save:  GY$8,370.41
ISK kr12,250.77
List Price:  ISK kr17,151.57
You save:  ISK kr4,900.80
DH907.55
List Price:  DH1,270.61
You save:  DH363.06
L1,676.34
List Price:  L2,346.95
You save:  L670.60
ден5,281.40
List Price:  ден7,394.17
You save:  ден2,112.77
MOP$803.54
List Price:  MOP$1,124.98
You save:  MOP$321.44
N$1,773.82
List Price:  N$2,483.42
You save:  N$709.60
C$3,680.31
List Price:  C$5,152.59
You save:  C$1,472.27
रु14,096.01
List Price:  रु19,734.98
You save:  रु5,638.96
S/352.21
List Price:  S/493.11
You save:  S/140.90
K423.82
List Price:  K593.36
You save:  K169.54
SAR375.17
List Price:  SAR525.26
You save:  SAR150.08
ZK2,380.02
List Price:  ZK3,332.12
You save:  ZK952.10
L434.52
List Price:  L608.35
You save:  L173.82
Kč2,090.29
List Price:  Kč2,926.49
You save:  Kč836.19
Ft33,616.37
List Price:  Ft47,064.26
You save:  Ft13,447.89
SEK kr940.94
List Price:  SEK kr1,317.36
You save:  SEK kr376.41
ARS$136,236.96
List Price:  ARS$190,737.20
You save:  ARS$54,500.23
Bs691.36
List Price:  Bs967.94
You save:  Bs276.57
COP$399,414.69
List Price:  COP$559,196.55
You save:  COP$159,781.85
₡50,561.46
List Price:  ₡70,788.07
You save:  ₡20,226.60
L2,620.33
List Price:  L3,668.57
You save:  L1,048.24
₲720,925.20
List Price:  ₲1,009,324.12
You save:  ₲288,398.92
$U4,006.62
List Price:  $U5,609.43
You save:  $U1,602.81
zł363.92
List Price:  zł509.51
You save:  zł145.58
Already have an account? Log In

Transcript

Now look at example if the one titled foo is just a function again. So foo is just back to being a function, it's not a constructor, I don't instantiate it. And on line 61, I learned speed equals 100. Or I say speed equals 100. And then on 63, I learned this dot speed. So copy that code and refresh the page and paste that code in your browser.

And we get 100. So up until now, in a lot of these examples we've gotten on defined but in this case, we get 100. And the reason is, if you remember that by omitting the var keyword, I wind up with an implied global. So once again, omitting the var keyword is pretty much the same thing as doing this, which is also the same exact thing as doing this, which is also the same exact thing as doing this. Line. 6061 and 62 are all the exact same thing, but regardless On line 63.

By omitting the var keyword, I create an implied global. And we create a property on the window object called speed, which means that on line 64, when we access this speed, we get six we get sick, we get 100. Because we're basically asking for window dot speed. So this is a case once again, where the delay line between scope and contact context can feel fuzzy. Because we're creating a variable here on line 62. There's no doubt about it.

But we're creating an implied global and implied Global's become properties of the window object in browser based JavaScript. So this is where its scope but in a way, it's also context, because we're reaching for the property of an object, and we can access this inside of this function and reference the window object

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.