aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/errors/cant_delete/index.html
blob: 2e2a44b27d8f91882abf12ab3a3f88623229b653 (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
---
title: 'TypeError: property "x" is non-configurable and can''t be deleted'
slug: Web/JavaScript/Reference/Errors/Cant_delete
tags:
  - Errors
  - JavaScript
  - Strict Mode
  - TypeError
translation_of: Web/JavaScript/Reference/Errors/Cant_delete
---
<div>{{jsSidebar("Errors")}}</div>

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

<pre class="syntaxbox">TypeError: property "x" is non-configurable and can't be deleted. (Firefox)
TypeError: Cannot delete property 'x' of #&lt;Object&gt; (Chrome)
</pre>

<h2 id="エラータイプ">エラータイプ</h2>

<p>strict モードでのみ、{{jsxref("TypeError")}} の警告が出ます。</p>

<h2 id="何がうまくいかなかったのか?">何がうまくいかなかったのか?</h2>

<p>プロパティを削除しようとしましたが、プロパティが <a href="/ja/docs/Web/JavaScript/Data_structures#Properties">non-configurable</a> でした。<code>configurable</code> 属性は、オブジェクトからプロパティを削除できるかどうか、および(<code>writable</code> を除く)属性を変更できるかどうかを制御します。</p>

<p>このエラーは、<a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict モードのコード</a> でのみ発生します。非 strict コードでは、この操作は <code>false</code> を返します。</p>

<h2 id="例"></h2>

<p>non-configurable プロパティは、さほど一般的ではありませんが、{{jsxref("Object.defineProperty()")}}{{jsxref("Object.freeze()")}} を使用して生成できます。</p>

<pre class="brush: js example-bad">"use strict";
var obj = Object.freeze({name: "Elsa", score: 157});
delete obj.score;  // TypeError

"use strict";
var obj = {};
Object.defineProperty(obj, "foo", {value: 2, configurable: false});
delete obj.foo;  // TypeError

"use strict";
var frozenArray = Object.freeze([0, 1, 2]);
frozenArray.pop();  // TypeError
</pre>

<p>JavaScript に組み込まれた、少数の non-configurable プロパティもあります。もしかしたら、Math の定数を削除しようとしたのかもしれません。</p>

<pre class="brush: js example-bad">"use strict";
delete Math.PI;  // TypeError</pre>

<h2 id="関連項目">関連項目</h2>

<ul>
 <li><a href="/ja/docs/Web/JavaScript/Reference/Operators/delete">delete</a></li>
 <li>{{jsxref("Object.defineProperty()")}}</li>
 <li>{{jsxref("Object.freeze()")}}</li>
</ul>