aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/errors/not_defined/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-tw/web/javascript/reference/errors/not_defined/index.html')
-rw-r--r--files/zh-tw/web/javascript/reference/errors/not_defined/index.html67
1 files changed, 67 insertions, 0 deletions
diff --git a/files/zh-tw/web/javascript/reference/errors/not_defined/index.html b/files/zh-tw/web/javascript/reference/errors/not_defined/index.html
new file mode 100644
index 0000000000..fa79033977
--- /dev/null
+++ b/files/zh-tw/web/javascript/reference/errors/not_defined/index.html
@@ -0,0 +1,67 @@
+---
+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>注意:</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>A variable need to be available in the current context of execution. Variables defined inside a <a href="/en-US/docs/Web/JavaScript/Reference/Functions">function</a> cannot be accessed from anywhere outside the function, because the variable is defined only in the scope of the function</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>However, a function can access all variables and functions defined inside the scope in which it is defined. In other words, a function defined in the global scope can access all variables defined in the global scope.</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="/zh-TW/docs/Web/JavaScript/Guide/Grammar_and_types#Declaring_variables">Declaring variables in the JavaScript Guide</a></li>
+ <li><a href="/zh-TW/docs/Web/JavaScript/Guide/Functions#Function_scope/en-US/docs/">Function scope in the JavaScript Guide</a></li>
+</ul>