mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-09 15:34:05 +08:00
64 lines
6.5 KiB
HTML
64 lines
6.5 KiB
HTML
<article id="wikiArticle">
|
||
<div></div>
|
||
<h2 id="信息">信息</h2>
|
||
<pre><code class="language-javascript">TypeError: "x" is not a constructor
|
||
|
||
TypeError: Math is not a constructor
|
||
TypeError: JSON is not a constructor
|
||
TypeError: Symbol is not a constructor
|
||
TypeError: Reflect is not a constructor
|
||
TypeError: Intl is not a constructor
|
||
TypeError: SIMD is not a constructor
|
||
TypeError: Atomics is not a constructor
|
||
</code></pre>
|
||
<h2 id="错误类型">错误类型</h2>
|
||
<p><a href="Reference/Global_Objects/TypeError" title="TypeError(类型错误) 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a></p>
|
||
<h2 id="哪里出错了">哪里出错了?</h2>
|
||
<p>是因为尝试将不是构造器的对象或者变量来作为构造器使用。参考 <a class="glossaryLink" href="/en-US/docs/Glossary/constructor" title="constructor: A constructor belongs to a particular class object that is instantiated. The constructor initializes this object and can provide access to its private information. The concept of a constructor can be applied to most object-oriented programming languages. Essentially, a constructor in JavaScript is usually declared at the instance of a class.">constructor</a> 或者 <a href="Reference/Operators/new"><code>new</code> operator</a> 来了解什么是构造器。</p>
|
||
<p>有很多的全局对象比如 <a href="Reference/String" title="此页面仍未被本地化, 期待您的翻译!"><code>String</code></a>、<a href="Reference/Array" title="REDIRECT Array"><code>Array</code></a> 等等都是可以使用 <code>new</code> 操作符的构造器。但是有一些全局对象并不是,且其属性和方法都是<a class="external" href="https://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods" rel="noopener">静态</a>的。下面的 JavaScript 标准内置对象都不是构造器:<a href="Reference/Global_Objects/Math" title="Math 是一个内置对象, 它具有数学常数和函数的属性和方法。不是一个函数对象。"><code>Math</code></a>,<a href="Reference/Global_Objects/JSON" title="JSON对象包含两个方法: 用于解析 JavaScript Object Notation (JSON) 的 parse() 方法,以及将对象/值转换为 JSON字符串的 stringify() 方法。除了这两个方法, JSON这个对象本身并没有其他作用,也不能被调用或者作为构造函数调用。"><code>JSON</code></a>,<a href="Reference/Global_Objects/Symbol" title='Symbol()函数会返回symbol类型的值,该类型具有静态属性和静态方法。它的静态属性会暴露几个内建的成员对象;它的静态方法会暴露全局的symbol注册,且类似于内建对象类,但作为构造函数来说它并不完整,因为它不支持语法:"new Symbol()"。'><code>Symbol</code></a>,<a href="Reference/Global_Objects/Reflect" title="Reflect 是一个内置的对象,它提供拦截 JavaScript 操作的方法。这些方法与处理器对象的方法相同。Reflect不是一个函数对象,因此它是不可构造的。"><code>Reflect</code></a>,<a href="Reference/Global_Objects/Intl" title="国际化的构造函数和其他构造函数的几个语言敏感的方法(可见See also)一样,使用同样的模式来识别语言区域和确定使用哪一种语言格式:他们都接收 locales 和 options 参数,使用 options.localeMatcher 属性指定的一个算法来对比应用请求的和支持的语言区域,来确定使用哪一个语言区域。"><code>Intl</code></a>,<a class="new" href="Reference/SIMD" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>SIMD</code></a>,<a href="Reference/Global_Objects/Atomics" title="Atomics 对象提供了一组静态方法用来对 SharedArrayBuffer 对象进行原子操作。"><code>Atomics</code></a>。</p>
|
||
<p><a href="Reference/Statements/function*">Generator functions</a> 也不能作为构造器来使用。</p>
|
||
<h2 id="示例">示例</h2>
|
||
<h3 id="无效的">无效的</h3>
|
||
<pre><code class="language-js example-bad">var Car = 1;
|
||
new Car();
|
||
// TypeError: Car is not a constructor
|
||
|
||
new Math();
|
||
// TypeError: Math is not a constructor
|
||
|
||
new Symbol();
|
||
// TypeError: Symbol is not a constructor
|
||
|
||
function* f() {};
|
||
var obj = new f;
|
||
// TypeError: f is not a constructor
|
||
</code></pre>
|
||
<h3 id="一个构造器">一个构造器</h3>
|
||
<p>假设你想为汽车创建一个对象类型。 你希望此类型的对象被称为 <code>car</code>,并且您希望它具有make,model 和 year 属性。 为此,你编写以下函数:</p>
|
||
<pre><code class="language-javascript">function Car(make, model, year) {
|
||
this.make = make;
|
||
this.model = model;
|
||
this.year = year;
|
||
}
|
||
</code></pre>
|
||
<p>现在你可以创建一个名为 <code>mycar</code> 的对象,如下所示:</p>
|
||
<pre><code class="language-javascript">var mycar = new Car("Eagle", "Talon TSi", 1993);</code></pre>
|
||
<h3 id="关于_Promises">关于 Promises </h3>
|
||
<p>当返回了一个 immediately-resolved 或者 immediately-rejected Promise 的时候,你根本不需要去创建、操作一个新的 Promise 对象。</p>
|
||
<p>这是不合法的(<a href="/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Promise#Constructor">Promise constructor</a> 被错误的调用了)且会抛出一个 错误 <code>TypeError: this is not a constructor</code> exception:</p>
|
||
<pre><code class="language-js example-bad">return new Promise.resolve(true);
|
||
</code></pre>
|
||
<p>使用<a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve"> Promise.resolve()</a> 或者 <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject">Promise.reject()</a> 静态方法来代替:</p>
|
||
<pre><code class="language-javascript">// 这是合法的,但是没必要这么长:
|
||
return new Promise((resolve, reject) => { resolve(true); })
|
||
|
||
// 用静态方法来代替:
|
||
return Promise.resolve(true);
|
||
return Promise.reject(false);
|
||
</code></pre>
|
||
<h2 id="相关链接">相关链接</h2>
|
||
<ul>
|
||
<li><a class="glossaryLink" href="/en-US/docs/Glossary/constructor" title="constructor: A constructor belongs to a particular class object that is instantiated. The constructor initializes this object and can provide access to its private information. The concept of a constructor can be applied to most object-oriented programming languages. Essentially, a constructor in JavaScript is usually declared at the instance of a class.">constructor</a></li>
|
||
<li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/new"><code>new</code> operator</a></li>
|
||
</ul>
|
||
</article> |