Global Scope and Functions - Free Code Camp

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

I passed this test without any help, but I still didn't understand what the hell I accomplished

rodriguejean
Автор

I THINK the point of this lesson is that by not using var to create oopsGlobal it "accidentally" has global scope (same as myGlobal) and therefore is visible to fun2. 

When you declare oopsGlobal with var fun2 no longer has an output for it on the console - presumably because oopsGlobal no longer has global scope as it is declared correctly and is within a function.

To pass the test you have to do something "incorrect" and then not knowing what all the additional code is doing makes this lesson as confusing as hell!

This was my takeaway anyway (having done it 3 times and watched this video then tried to understand what fun2 was doing). But this is all new to me so could be waaaay off.

Really appreciates these videos by the way 👍🏻

mezzdavies
Автор

2022 update
// Declare the myGlobal variable below this line
const myGlobal = 10;

function fun1() {
// Assign 5 to oopsGlobal Here
oopsGlobal = 5;
}

// Only change code above this line

function fun2() {
var output = "";
if (typeof myGlobal != "undefined") {
output += "myGlobal: " + myGlobal;
}
if (typeof oopsGlobal != "undefined") {
output += " oopsGlobal: " + oopsGlobal;
}
console.log(output);
}

forestkane_
Автор

I've come to understand Global meaning that it's a variable that you want to pass into more than one function such as the player's info (name, age, etc)... whereas Local is explicitly within one specific function. From what I gather... initializing a variable with "var" marks it as a local variable only, whereas not using "var" makes it known as a global variable.

...I made a mod (The Martian Tribune) for a game (Surviving Mars) some time ago. I got completely lambasted by the modding community when I asked for assistance on occasion due to my overuse of globals. I made their names VERY specific, but apparently global usage must be kept to an absolute minimum. Recognizing that my variables became available (for use and accidental alteration) across the ENTIRE game's code helps me understand why they were so annoyed, now. :-p

chetranqui
Автор

I got the answer correct Mr. Ian yet this is still a tricky lesson so it only make sense for me to practice it more and more to get used to it.

zken
Автор

Very confusing.
I thought: typeof myGlobal(a number) is Not= to "undefined" (a string) which is correct, so it outputs.
The "undefined" is the trickiy one.
Stiill, if i change the strings to a number, it should give a false . And it doesn't :/
Anyways, it is very nice get through these tutorials with you Ian., Thanks!

AsafEfraim-um
Автор

I had a feeling this particular tutorial was wrong. My understanding of global variables; If you declare a variable outside of a function, it is a global variable. Meaning you can use that variable anywhere else. but when you declare a variable within a function you can only use it inside that function.

successimamun
Автор

This fried my brain. Appreciate your videos though!

jonnien
Автор

// Declare the myGlobal variable below this line
const myGlobal = 10;

function fun1() {
// Assign 5 to oopsGlobal Here
oopsGlobal = 5;
}

// Only change code above this line

function fun2() {
var output = "";
if (typeof myGlobal != "undefined") {
output += "myGlobal: " + myGlobal;
}
if (typeof oopsGlobal != "undefined") {
output += " oopsGlobal: " + oopsGlobal;
}
console.log(output);
}
console.log(oopsGlobal)

zheng
Автор

Beu does a bad job at making Javascript enjoyable

bvsanchez