aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/errors/cant_access_property/index.html
blob: a2fc8a56b4929115482ec820a52bd148c3385117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
---
title: 'TypeError: can''t access property "x" of "y"'
slug: Web/JavaScript/Reference/Errors/Cant_access_property
tags:
- Error
- Errors
- JavaScript
- TypeError
translation_of: Web/JavaScript/Reference/Errors/Cant_access_property
---
<div>{{jsSidebar("Errors")}}</div>

<p>JavaScript の例外 "can't access property" は、 {{jsxref("undefined")}} または {{jsxref("null")}} の値に対してプロパティのアクセスが行われた場合に発生します。</p>

<h2 id="Message">エラーメッセージ</h2>

<pre class="brush: js">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)

例:
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="Error_type">エラーの種類</h2>

<p>{{jsxref("TypeError")}}</p>

<h2 id="What_went_wrong">エラーの原因</h2>

<p>{{jsxref("undefined")}}{{jsxref("null")}} に対してプロパティアクセスを行いました。</p>

<h2 id="Examples"></h2>

<h3 id="Invalid_cases">無効な場合</h3>

<pre class="brush: js example-bad">// undefined and null cases on which the substring method won't work
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="Fixing_the_issue">問題の修正</h3>

<p><code>undefined</code><code>null</code> のヌルポインターアクセスを修正するには、たとえば <a href="/ja/docs/Web/JavaScript/Reference/Operators/typeof">typeof</a> 演算子を使用することができます。</p>

<pre class="brush: js">if (typeof foo !== 'undefined') {
  // Now we know that foo is defined, we are good to go.
}</pre>

<h2 id="See_also">関連情報</h2>

<ul>
  <li>{{jsxref("undefined")}}</li>
  <li>{{jsxref("null")}}</li>
</ul>