2019-04-21 11:50:48 +08:00

44 lines
3.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<h2 id="错误信息">错误信息</h2>
<pre><code class="language-javascript">ReferenceError: "x" is not defined
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/ReferenceError" title="ReferenceError引用错误 对象代表当一个不存在的变量被引用时发生的错误。"><code>ReferenceError</code></a>.</p>
<h2 id="什么地方出错了">什么地方出错了?</h2>
<p>在某些地方引用一个不存在的变量的时候。当你使用变量的时候,这个变量必须是已经被声明的,或者你可以确保它在你当前的脚本或作用域 (<a class="glossaryLink" href="/en-US/docs/Glossary/scope" title='scope: The current context of execution. The context in which values and expressions are "visible," or can be referenced. If a variable or other expression is not "in the current scope," then it is unavailable for use. Scopes can also be layered in a hierarchy, so that child scopes have access to parent scopes, but not vice versa.'>scope</a>) 中可用。</p>
<div class="note">
<p><strong>注意:</strong> 当你加载一个库的时候(例如 jQuery请确保你在这个库加载完毕后再使用这个库中的变量如“$”。将你想加载的库的 <a href="/zh-CN/docs/Web/HTML/Element/script" title="HTML &lt;script&gt; 元素用于嵌入或引用可执行脚本。"><code>&lt;script&gt;</code></a> 标签放置在你的代码前面。</p>
</div>
<h2 id="示例">示例</h2>
<h3 id="变量没有被声明">变量没有被声明</h3>
<pre><code class="language-js example-bad">foo.substring(1); // ReferenceError: foo is not defined
</code></pre>
<p>“foo” 变量没有在任何地方被声明。它需要是某种字符串,这样 <a href="Reference/Global_Objects/String/substring" title="substring() 方法返回一个字符串在开始索引到结束索引之间的一个子集, 或从开始索引直到字符串的末尾的一个子集。"><code>String.prototype.substring()</code></a> 方法才可以正常工作。</p>
<pre><code class="language-js example-good">var foo = 'bar';
foo.substring(1); // "ar"</code></pre>
<h3 id="错误的作用域">错误的作用域</h3>
<p>变量必须是在它当前的执行环境中可用的。在一个函数(<a href="/en-US/docs/Web/JavaScript/Reference/Functions">function</a>)中定义的变量不能从这个函数外部的任何地方访问,因为这个变量的作用域仅在这个函数的内部。</p>
<pre><code class="language-js example-bad">function numbers () {
var num1 = 2,
num2 = 3;
return num1 + num2;
}
console.log(num1); // ReferenceError num1 is not defined.</code></pre>
<p>然而,一个函数可用使用在它所被定义的作用域中的所有变量。换句话说,当一个函数被定义在全局作用域的时候,它可以访问所有在全局作用域中定义的变量。</p>
<pre><code class="language-js example-good">var num1 = 2,
num2 = 3;
function numbers () {
return num1 + num2;
}
console.log(num1); // 2</code></pre>
<h2 id="相关页面">相关页面</h2>
<ul>
<li><a class="glossaryLink" href="/en-US/docs/Glossary/Scope" title='Scope: The current context of execution. The context in which values and expressions are "visible," or can be referenced. If a variable or other expression is not "in the current scope," then it is unavailable for use. Scopes can also be layered in a hierarchy, so that child scopes have access to parent scopes, but not vice versa.'>Scope</a></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>
</article>