aboutsummaryrefslogtreecommitdiff
path: root/files/ko/web/javascript/reference/errors/not_defined/index.html
blob: 6b403dd848408b6abeb2b223b1a49ba97f1e510e (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
---
title: 'ReferenceError: "x" is not defined'
slug: Web/JavaScript/Reference/Errors/Not_defined
translation_of: Web/JavaScript/Reference/Errors/Not_defined
---
<div>{{jsSidebar("Errors")}}</div>

<h2 id="메시지">메시지</h2>

<pre class="syntaxbox">ReferenceError: "x" is not defined
</pre>

<h2 id="에러_타입">에러 타입</h2>

<p>{{jsxref("ReferenceError")}}.</p>

<h2 id="무엇이_잘못되었을까">무엇이 잘못되었을까?</h2>

<p>존재하지 않는 변수를 참조하는 곳이 있습니다. 이 변수는 선언되어야 합니다. 또는, 현재 스크립트나 {{Glossary("scope")}} 에서 사용이 가능하도록 해야합니다.</p>

<div class="note">
<p><strong>Note:</strong> 라이브러리(예를 들면 jQuery와 같은)의 로딩은, 반드시 코드에서  "$"와 같은 라이브러리 변수에 접근하기 이전에 수행되어야 합니다. 라이브러리를 로딩하는 {{HTMLElement("script")}} 태그가 그 변수를 사용하는 코드보다 앞에 위치하도록 하세요.</p>
</div>

<h2 id="예"></h2>

<h3 id="선언되지_않은_변수">선언되지 않은 변수</h3>

<pre class="brush: js example-bad">foo.substring(1); // ReferenceError: foo is not defined
</pre>

<p>"foo" 변수는 어디에도 선언되지 않았습니다. {{jsxref("String.prototype.substring()")}} 메소드가 작동하도록 하기 위해서는 문자열을 필요로 합니다.</p>

<pre class="brush: js example-good">var foo = "bar";
foo.substring(1); // "ar"</pre>

<h3 id="잘못된_스코프">잘못된 스코프</h3>

<p>변수는 현재의 실행 흐름 내에서 이용 가능해야합니다. 함수 내부에 정의된 변수는 다른 외부의 함수에서는 접근할 수 없습니다. 그 때문에, 변수는 함수의 스코프 내부에서만 정의 됩니다.</p>

<pre class="brush: js example-bad">function numbers () {
  var num1 = 2,
      num2 = 3;
  return num1 + num2;
}

console.log(num1); // ReferenceError: num1 is not defined</pre>

<p>그러나, 함수는 모든 변수와 정의된 스코프 안에 정의된 함수에 접근할 수 있습니다. 따라서, 전역으로 정의된 함수는 전역에 정의된 모든 변수에도 접근할 수 있습니다.</p>

<pre class="brush: js example-good">var num1 = 2,
    num2 = 3;

function numbers () {
  return num1 + num2;
}

console.log(num1); // 2</pre>

<h2 id="참조">참조</h2>

<ul>
 <li>{{Glossary("Scope")}}</li>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Declaring_variables">Declaring variables in the JavaScript Guide</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Functions#Function_scope/en-US/docs/">Function scope in the JavaScript Guide</a></li>
</ul>