Repository navigation
Expand file tree
/
Copy pathindex.md
More file actions
74 lines (51 loc) · 2.51 KB
/
Copy pathindex.md
File metadata and controls
74 lines (51 loc) · 2.51 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
---
title: ReferenceError
slug: Web/JavaScript/Reference/Global_Objects/ReferenceError
l10n:
sourceCommit: 6558de67a347fee30c303da8a0b262a9270a6885
---
{{JSRef}}
**`ReferenceError`** オブジェクトは、現在のスコープに存在しない(あるいはまだ初期化されていない)変数が参照されたときのエラーを表します。
`ReferenceError` は{{Glossary("serializable object", "シリアライズ可能オブジェクト")}}なので、 {{domxref("structuredClone()")}} で複製したり、[ワーカー](/ja/docs/Web/API/Worker)間で {{domxref("Worker/postMessage()", "postMessage()")}} を使用してコピーしたりすることができます。
`ReferenceError` は {{jsxref("Error")}} のサブクラスです。
## コンストラクター
- {{jsxref("ReferenceError/ReferenceError", "ReferenceError()")}}
- : 新しい `ReferenceError` オブジェクトを生成します。
## インスタンスプロパティ
_親である {{jsxref("Error")}} から継承したプロパティもあります。_
これらのプロパティは `ReferenceError.prototype` で定義されており、すべての `ReferenceError` インスタンスで共有されます。
- {{jsxref("Object/constructor", "ReferenceError.prototype.constructor")}}
- : このインスタンスオブジェクトを作成したコンストラクター関数。 `ReferenceError` インスタンスの場合、初期値は {{jsxref("ReferenceError/ReferenceError", "ReferenceError")}} コンストラクタです。
- {{jsxref("Error/name", "ReferenceError.prototype.name")}}
- : エラー型の名前を表します。 `ReferenceError.prototype.name` の初期値は `"ReferenceError"` です。
## インスタンスメソッド
_親である {{jsxref("Error")}} から継承したメソッドもあります。_
## 例
### ReferenceError の捕捉
```js
try {
let a = undefinedVariable;
} catch (e) {
console.log(e instanceof ReferenceError); // true
console.log(e.message); // "undefinedVariable is not defined"
console.log(e.name); // "ReferenceError"
console.log(e.stack); // このエラーのスタック
}
```
### ReferenceError の生成
```js
try {
throw new ReferenceError("Hello");
} catch (e) {
console.log(e instanceof ReferenceError); // true
console.log(e.message); // "Hello"
console.log(e.name); // "ReferenceError"
console.log(e.stack); // このエラーのスタック
}
```
## 仕様書
{{Specifications}}
## ブラウザーの互換性
{{Compat}}
## 関連情報
- {{jsxref("Error")}}