diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/reflect/construct/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/reflect/construct/index.html | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/reflect/construct/index.html b/files/zh-cn/web/javascript/reference/global_objects/reflect/construct/index.html new file mode 100644 index 0000000000..8c6c4e2126 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/reflect/construct/index.html @@ -0,0 +1,189 @@ +--- +title: Reflect.construct() +slug: Web/JavaScript/Reference/Global_Objects/Reflect/construct +translation_of: Web/JavaScript/Reference/Global_Objects/Reflect/construct +--- +<div>{{JSRef}}</div> + +<p><code><strong>Reflect</strong></code><strong><code>.construct()</code></strong> 方法的行为有点像 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new"><code>new</code> 操作符</a> 构造函数 , 相当于运行 <code>new target(...args)</code>.</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate">Reflect.construct(target, argumentsList[, newTarget]) +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>target</code></dt> + <dd>被运行的目标构造函数</dd> + <dt><code>argumentsList</code></dt> + <dd>类数组,目标构造函数调用时的参数。</dd> + <dt><code>newTarget</code> {{optional_inline}}</dt> + <dd>作为新创建对象的原型对象的<code>constructor</code>属性, 参考 <code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/new.target">new.target</a></code> 操作符,默认值为<code>target。</code></dd> +</dl> + +<h3 id="返回值"><code>返回值</code></h3> + +<p>以<code>target</code>(如果<code>newTarget</code>存在,则为<code>newTarget</code>)函数为构造函数,<code>argumentList</code>为其初始化参数的对象实例。</p> + +<h3 id="异常">异常</h3> + +<p>如果target或者newTarget不是构造函数,抛出{{jsxref("TypeError")}},异常。</p> + +<h2 id="描述">描述</h2> + +<p><code>Reflect.construct</code>允许你使用可变的参数来调用构造函数 ,这和使用<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/new">new操作符</a>搭配<a href="https://developer.mozilla.org/ zh-CN/docs/Web/JavaScript/Reference/Operators/new">对象展开符</a>调用一样。</p> + +<pre class="brush: js notranslate">var obj = new Foo(...args); +var obj = Reflect.construct(Foo, args); </pre> + +<h3 id="Reflect.construct_vs_Object.create"><code>Reflect.construct()</code> vs <code>Object.create()</code></h3> + +<p><code><font face="Arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">在新语法</span></font>Reflect</code>出现之前,是通过明确指定构造函数和原型对象( 使用{{jsxref("Object.create()")}})来创建一个对象的。</p> + +<pre class="brush: js notranslate">function OneClass() { + this.name = 'one'; +} + +function OtherClass() { + this.name = 'other'; +} + +// 创建一个对象: +var obj1 = Reflect.construct(OneClass, args, OtherClass); + +// 与上述方法等效: +var obj2 = Object.create(OtherClass.prototype); +OneClass.apply(obj2, args); + +console.log(obj1.name); // 'one' +console.log(obj2.name); // 'one' + +console.log(obj1 instanceof OneClass); // false +console.log(obj2 instanceof OneClass); // false + +console.log(obj1 instanceof OtherClass); // true +console.log(obj2 instanceof OtherClass); // true</pre> + +<p>虽然两种方式结果相同,但在创建对象过程中仍一点不同。 </p> + +<p><font face="Arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">当使用</span></font><code>Object.create()</code>和{{jsxref("Function.prototype.apply()")}}时,如果不使用<code>new</code>操作符调用构造函数,构造函数内部的<code>new.target</code>值会指向<code>undefined</code>。</p> + +<p>当调用<code>Reflect.construct()</code>来创建对象,<code>new.target</code>值会自动指定到<code>target</code>(或者newTarget,前提是newTarget指定了)。</p> + +<pre class="brush: js notranslate">function OneClass() { + console.log('OneClass'); + console.log(new.target); +} +function OtherClass() { + console.log('OtherClass'); + console.log(new.target); +} + +var obj1 = Reflect.construct(OneClass, args); +// 输出: +// OneClass +// function OneClass { ... } + +var obj2 = Reflect.construct(OneClass, args, OtherClass); +// 输出: +// OneClass +// function OtherClass { ... } + +var obj3 = Object.create(OtherClass.prototype); +OneClass.apply(obj3, args); +// 输出: +// OneClass +// undefined</pre> + +<h2 id="举例"><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif"><span style="font-size: 37.33327865600586px;"><strong>举例</strong></span></font></h2> + +<h3 id="使用_Reflect.construct">使用 <code>Reflect.construct()</code></h3> + +<pre class="brush: js notranslate">var d = Reflect.construct(Date, [1776, 6, 4]); +d instanceof Date; // true +d.getFullYear(); // 1776 +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-reflect.construct', 'Reflect.construct')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-reflect.construct', 'Reflect.construct')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>49</td> + <td>{{CompatGeckoDesktop(42)}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile(42)}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Reflect")}}</li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new"><code>new</code></a></li> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/new.target">new.target</a></code></li> +</ul> |