mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-09 15:34:05 +08:00
38 lines
2.5 KiB
HTML
38 lines
2.5 KiB
HTML
<article id="wikiArticle">
|
||
<div></div>
|
||
<h2 id="消息">消息</h2>
|
||
<pre><code class="language-javascript">TypeError: Unable to get property {x} of undefined or null reference (Edge)
|
||
TypeError: can't access property {x} of {y} (Firefox)
|
||
TypeError: {y} is undefined, can't access property {x} of it (Firefox)
|
||
TypeError: {y} is null, can't access property {x} of it (Firefox)
|
||
|
||
Examples:
|
||
TypeError: x is undefined, can't access property "prop" of it
|
||
TypeError: x is null, can't access property "prop" of it
|
||
TypeError: can't access property "prop" of undefined
|
||
TypeError: can't access property "prop" of null
|
||
</code></pre>
|
||
<h2 id="错误类型">错误类型</h2>
|
||
<p><a href="Reference/Global_Objects/TypeError" title="TypeError(类型错误) 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a>.</p>
|
||
<h2 id="哪里出错了">哪里出错了?</h2>
|
||
<p>访问了 <a href="Reference/Global_Objects/undefined" title="undefined是全局对象的一个属性。也就是说,它是全局作用域的一个变量。undefined的最初值就是原始数据类型undefined。"><code>undefined</code></a> 或<a href="Reference/Global_Objects/null" title="值 null 特指对象的值未设置。它是 JavaScript 基本类型 之一。"><code>null</code></a> 值的属性.</p>
|
||
<h2 id="例子">例子</h2>
|
||
<h3 id="无效情形">无效情形</h3>
|
||
<pre><code class="language-js example-bad">// 对于undefined 以及 null 值, substring 方法无法正常工作
|
||
var foo = undefined;
|
||
foo.substring(1); // TypeError: x is undefined, can't access property "substring" of it
|
||
|
||
var foo = null;
|
||
foo.substring(1); // TypeError: x is null, can't access property "substring" of it
|
||
</code></pre>
|
||
<h3 id="解决问题">解决问题</h3>
|
||
<p>为了解决<code>undefined</code> 或 <code>null</code> 值的空指针问题,你可以使用 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/typeof">typeof</a> 操作符, 比如.</p>
|
||
<pre><code class="language-javascript">if (typeof foo !== 'undefined') {
|
||
// 现在已经确认foo已定义,可以进一步操作.
|
||
}</code></pre>
|
||
<h2 id="参考更多">参考更多</h2>
|
||
<ul>
|
||
<li><a href="Reference/Global_Objects/undefined" title="undefined是全局对象的一个属性。也就是说,它是全局作用域的一个变量。undefined的最初值就是原始数据类型undefined。"><code>undefined</code></a></li>
|
||
<li><a href="Reference/Global_Objects/null" title="值 null 特指对象的值未设置。它是 JavaScript 基本类型 之一。"><code>null</code></a></li>
|
||
</ul>
|
||
</article> |