aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/errors/no_non-null_object/index.md
blob: b548d38c6aec8cc495bd9239b037b8ce789ae911 (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
63
64
65
66
67
68
69
70
71
72
73
---
title: 'TypeError: "x" is not a non-null object'
slug: Web/JavaScript/Reference/Errors/No_non-null_object
tags:
- Error
- Errors
- JavaScript
- TypeError
translation_of: Web/JavaScript/Reference/Errors/No_non-null_object
---
{{JSSidebar("Errors")}}

JavaScript の例外 "is not a non-null object" は、ある場所でオブジェクトが期待されているのに提供されなかった場合に発生します。 {{jsxref("null")}} はオブジェクトではなく、動作しません。

## エラーメッセージ

```js
TypeError: Invalid descriptor for property {x} (Edge)
TypeError: "x" is not a non-null object (Firefox)
TypeError: Property description must be an object: "x" (Chrome)
TypeError: Invalid value used in weak set (Chrome)
```

## エラーの種類

{{jsxref("TypeError")}}

## エラーの原因

ある場所でオブジェクトが期待されていますが、提供されませんでした。 {{jsxref("null")}} はオブジェクトではなく、動作しません。与えられた状況で適切なオブジェクトを提供しなければなりません。

## 例

## プロパティ記述子が求められている場合

{{jsxref("Object.create()")}} メソッドや {{jsxref("Object.defineProperty()")}} メソッド、{{jsxref("Object.defineProperties()")}} メソッドを使用するとき、省略可能な記述子の引数として、プロパティ記述子オブジェクトが想定されます。 (ただの数値など) オブジェクト以外のものを提供すると、エラーが発生します。

```js example-bad
Object.defineProperty({}, 'key', 1);
// TypeError: 1 is not a non-null object

Object.defineProperty({}, 'key', null);
// TypeError: null is not a non-null object
```

有効なプロパティ記述子はこのようになります。

```js example-good
Object.defineProperty({}, 'key', { value: 'foo', writable: false });
```

## `WeakMap` および `WeakSet` オブジェクトにはオブジェクトキーが必要

{{jsxref("WeakMap")}} および {{jsxref("WeakSet")}} オブジェクトはオブジェクトをキーとして保持します。そのほかの型をキーとして使用できません。

```js example-bad
var ws = new WeakSet();
ws.add('foo');
// TypeError: "foo" is not a non-null object
```

代わりにオブジェクトを使用してください。

```js example-good
ws.add({foo: 'bar'});
ws.add(window);
```

## 関連項目

- {{jsxref("Object.create()")}}
- {{jsxref("Object.defineProperty()")}}, {{jsxref("Object.defineProperties()")}}
- {{jsxref("WeakMap")}}, {{jsxref("WeakSet")}}