-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-02(Scope).html
More file actions
38 lines (32 loc) · 1.25 KB
/
3-02(Scope).html
File metadata and controls
38 lines (32 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<html>
<head>
<title>Scope</title>
</head>
<body>
<script>
// Scope : 변수에 대한 접근성을 의미
// function myFunction() {
// // local Scope : 지역 변수
// var carName1 = "현대자동차";
// console.log(carName1);
// }
// console.log(carName1); // 레퍼런스 에러 발생, 함수 내에서만 사용 O
// myFunction();
// global Scope : 전역 변수
var carName2 = "기아 자동차";
myFunction2(); // 호출 -> 선언, 에러 발생 X(function()은 먼저 컴파일을 실행)
function myFunction2() {
console.log(carName2);
}
// console.log(carName2);
// 함수 안에서 사용된 변수는 함수 밖 사용 X but 함수 밖에서 선언된 변수는 함수 안, 밖 둘다 사용 O
myFunction3(); // 에러 발생, myFunction3는 변수, function는 호출되기전에 미리 가지고 있음, 변수는 미리 컴파일 X 실행될 때 컴파일O
var myFunction3 = function() {
console.log("myFunction3 호출");
}
// function myFunction3() {
// console.log("myFunction3 호출");
// }
</script>
</body>
</html>