147. Scope

변지영·2022년 1월 13일
0
//scope : variable access, what variables do I have access to when a code is running?

var fun = 5;

function funFunction() {
	// child scope
	var fun = "hellooo";
	console.log(1, fun);
}

function funerFunction() {
	// child scope
	var fun = "Byee";
	console.log(2, fun);
}

function funestFunction() {
	// child scope
	fun = "AHHHHH";
	console.log(3, fun);
}

console.log("window", fun);

//scope : variable access, what variables do I have access to when a code is running?

var fun = 5;

function funFunction() {
	// child scope
	var fun = "hellooo";
	console.log(1, fun);
}

function funerFunction() {
	// child scope
	var fun = "Byee";
	console.log(2, fun);
}

function funestFunction() {
	// child scope
	fun = "AHHHHH";
	console.log(3, fun);
}

console.log("window", fun);
funFunction();
funerFunction();
funestFunction();

Look at this.

//Root Scope (window)
var fun = 5;

function funestFunction(){
	//child scope
	console.log(fun);
}

If we run funestFunction,
-console.log(fun): Do you know what fun is?
-child scope: sorry, I don't really know fun but ask my parent.

So now we go into outside of the function.
In this case, root scope.

Do you know fun?
-root scope: yah! We have a variable fun. It equals 5.

Now 'console.log' can log 5.
If fun doesn't exist. then we get an error.
So the last check is always the root scope.

0개의 댓글