aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/errors/not_defined
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/errors/not_defined
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/errors/not_defined')
-rw-r--r--files/zh-cn/web/javascript/reference/errors/not_defined/index.html66
1 files changed, 66 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/errors/not_defined/index.html b/files/zh-cn/web/javascript/reference/errors/not_defined/index.html
new file mode 100644
index 0000000000..a092f394ec
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/errors/not_defined/index.html
@@ -0,0 +1,66 @@
+---
+title: 'ReferenceError: "x" is not defined'
+slug: Web/JavaScript/Reference/Errors/Not_defined
+translation_of: Web/JavaScript/Reference/Errors/Not_defined
+---
+<p>{{jsSidebar("Errors")}}</p>
+
+<h2 id="错误信息">错误信息</h2>
+
+<pre class="syntaxbox notranslate">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 notranslate">foo.substring(1); // ReferenceError: foo is not defined
+</pre>
+
+<p>“foo” 变量没有在任何地方被声明。它需要是某种字符串,这样 {{jsxref("String.prototype.substring()")}} 方法才可以正常工作。</p>
+
+<pre class="brush: js example-good notranslate">var foo = 'bar';
+foo.substring(1); // "ar"</pre>
+
+<h3 id="错误的作用域">错误的作用域</h3>
+
+<p>变量必须是在它当前的执行环境中可用的。在一个函数(<a href="/en-US/docs/Web/JavaScript/Reference/Functions">function</a>)中定义的变量不能从这个函数外部的任何地方访问,因为这个变量的作用域仅在这个函数的内部。</p>
+
+<pre class="brush: js example-bad notranslate">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 notranslate">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>