aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/errors/cant_access_property
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/cant_access_property
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/cant_access_property')
-rw-r--r--files/zh-cn/web/javascript/reference/errors/cant_access_property/index.html55
1 files changed, 55 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/errors/cant_access_property/index.html b/files/zh-cn/web/javascript/reference/errors/cant_access_property/index.html
new file mode 100644
index 0000000000..8f8d830d2c
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/errors/cant_access_property/index.html
@@ -0,0 +1,55 @@
+---
+title: 'TypeError: can''t access property "x" of "y"'
+slug: Web/JavaScript/Reference/Errors/Cant_access_property
+translation_of: Web/JavaScript/Reference/Errors/Cant_access_property
+---
+<div>{{jsSidebar("Errors")}}</div>
+
+<h2 id="消息">消息</h2>
+
+<pre class="syntaxbox">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
+</pre>
+
+<h2 id="错误类型">错误类型</h2>
+
+<p>{{jsxref("TypeError")}}.</p>
+
+<h2 id="哪里出错了">哪里出错了?</h2>
+
+<p>访问了 {{jsxref("undefined")}} 或{{jsxref("null")}} 值的属性.</p>
+
+<h2 id="例子">例子</h2>
+
+<h3 id="无效情形">无效情形</h3>
+
+<pre class="brush: 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
+</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 class="brush: js">if (typeof foo !== 'undefined') {
+ // 现在已经确认foo已定义,可以进一步操作.
+}</pre>
+
+<h2 id="参考更多">参考更多</h2>
+
+<ul>
+ <li>{{jsxref("undefined")}}</li>
+ <li>{{jsxref("null")}}</li>
+</ul>