uTools-Manuals/docs/javascript/Reference/Errors/No_non-null_object.html

39 lines
3.8 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<h2 id="提示信息">提示信息</h2>
<pre class="syntaxbox">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)
</pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a></p>
<h2 id="哪里出错了?">哪里出错了?</h2>
<p>在期待出现对象类型的值的地方而没有提供。<a href="Reference/Global_Objects/null" title="值 null 特指对象的值未设置。它是 JavaScript 基本类型 之一。"><code>null</code></a> 不是对象类型,因此不起作用。必须在给定的场景下提供一个合适的对象。</p>
<h2 id="示例">示例</h2>
<h3 id="期望的属性描述器">期望的属性描述器</h3>
<p>当使用诸如 <a href="Reference/Global_Objects/Object/create" title="Object.create()方法创建一个新对象使用现有的对象来提供新创建的对象的__proto__。 (请打开浏览器控制台以查看运行结果。)"><code>Object.create()</code></a><a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a> 及{jsxref("Object.defineProperties()")}} 方法时,可选的属性描述器参数需要提供一个描述器对象。提供非对象类型的值(例如数字)将会报错:</p>
<pre class="brush: 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
</pre>
<p>一个合法的描述器对象类似于下面这样:</p>
<pre class="brush: js example-good">Object.defineProperty({}, 'key', { value: 'foo', writable: false });
</pre>
<h3 id="WeakMap_和_WeakSet_对象需要对象类型的键"><code>WeakMap</code> 和 <code>WeakSet</code> 对象需要对象类型的键</h3>
<p><a href="Reference/WeakMap" title="此页面仍未被本地化, 期待您的翻译!"><code>WeakMap</code></a><a href="Reference/Global_Objects/WeakSet" title="WeakSet 对象允许你将弱保持对象存储在一个集合中。"><code>WeakSet</code></a> 对象只能存储对象类型的键,而不能使用其他类型的。</p>
<pre class="brush: js example-bad">var ws = new WeakSet();
ws.add('foo');
// TypeError: "foo" is not a non-null object</pre>
<p>用对象类型的值来替换:</p>
<pre class="brush: js example-good">ws.add({foo: 'bar'});
ws.add(window);
</pre>
<h2 id="相关内容">相关内容</h2>
<ul>
<li><a href="Reference/Global_Objects/Object/create" title="Object.create()方法创建一个新对象使用现有的对象来提供新创建的对象的__proto__。 (请打开浏览器控制台以查看运行结果。)"><code>Object.create()</code></a></li>
<li><a href="Reference/Global_Objects/Object/defineProperty" title="Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。"><code>Object.defineProperty()</code></a>, <a href="Reference/Global_Objects/Object/defineProperties" title="Object.defineProperties() 方法直接在一个对象上定义新的属性或修改现有属性,并返回该对象。"><code>Object.defineProperties()</code></a></li>
<li><a href="Reference/WeakMap" title="此页面仍未被本地化, 期待您的翻译!"><code>WeakMap</code></a>, <a href="Reference/Global_Objects/WeakSet" title="WeakSet 对象允许你将弱保持对象存储在一个集合中。"><code>WeakSet</code></a></li>
</ul>
</article>