diff options
Diffstat (limited to 'files/pt-br/web/javascript/reference/global_objects/reflect/construct/index.html')
-rw-r--r-- | files/pt-br/web/javascript/reference/global_objects/reflect/construct/index.html | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/files/pt-br/web/javascript/reference/global_objects/reflect/construct/index.html b/files/pt-br/web/javascript/reference/global_objects/reflect/construct/index.html new file mode 100644 index 0000000000..dece94c79a --- /dev/null +++ b/files/pt-br/web/javascript/reference/global_objects/reflect/construct/index.html @@ -0,0 +1,151 @@ +--- +title: Reflect.construct() +slug: Web/JavaScript/Reference/Global_Objects/Reflect/construct +translation_of: Web/JavaScript/Reference/Global_Objects/Reflect/construct +--- +<div>{{JSRef}}</div> + +<p>The static <code><strong>Reflect</strong></code><strong><code>.construct()</code></strong> method acts like the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new"><code>new</code> operator</a>, but as a function. It is equivalent to calling <code>new target(...args)</code>. It gives also the added option to specify a different prototype.</p> + +<div>{{EmbedInteractiveExample("pages/js/reflect-construct.html")}}</div> + + + +<h2 id="Sintaxe">Sintaxe</h2> + +<pre class="syntaxbox">Reflect.construct(target, argumentsList[, newTarget]) +</pre> + +<h3 id="Parametros">Parametros</h3> + +<dl> + <dt><code>target</code></dt> + <dd>A função alvo à ser chamada.</dd> + <dt><code>argumentsList</code></dt> + <dd>Um objeto tipo array que especifica com quais argumentos <code>target</code> deveria ser chamada.</dd> + <dt><code>newTarget</code> {{optional_inline}}</dt> + <dd>O construtor de quem o protótipo deveria ser usado. Veja também o <code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/new.target">new.target</a></code> operador. Se <code>newTarget</code> não estiver presente, será <code>target</code>.</dd> +</dl> + +<h3 id="Return_value">Return value</h3> + +<p>A new instance of <code>target</code> (or <code>newTarget</code>, if present), initialized by <code>target</code> as a constructor with the given arguments.</p> + +<h3 id="Exceptions">Exceptions</h3> + +<p>A {{jsxref("TypeError")}}, if <code>target</code> or <code>newTarget</code> are not constructors.</p> + +<h2 id="Description">Description</h2> + +<p><code>Reflect.construct</code> allows you to invoke a constructor with a variable number of arguments (which would also be possible by using the <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator">spread operator</a> combined with the <a href="/en-US/docs/Web/JavaScript/Reference/Operators/new">new operator</a>).</p> + +<pre class="brush: js">var obj = new Foo(...args); +var obj = Reflect.construct(Foo, args); +</pre> + +<p> </p> + +<h3 id="Reflect.construct()_vs_Object.create()"><code>Reflect.construct()</code> vs <code>Object.create()</code></h3> + +<p>Prior to the introduction of <code>Reflect</code>, objects could be constructed using an arbitrary combination of constructor and prototype by using <code>Object.create()</code>.</p> + +<pre class="brush: js">function OneClass() { + this.name = 'one'; +} + +function OtherClass() { + this.name = 'other'; +} + +// Calling this: +var obj1 = Reflect.construct(OneClass, args, OtherClass); + +// ...has the same result as this: +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>However, while the end result is the same, there is one important difference in the process. When using <code>Object.create()</code> and <code>Function.prototype.apply()</code>, the <code>new.target</code> operator will point to <code>undefined</code> within the function used as the constructor, since the <code>new</code> keyword is not being used to create the object.</p> + +<p>When invoking <code>Reflect.construct()</code>, on the other hand, the <code>new.target</code> operator will point to the <code>newTarget</code> parameter if supplied, or <code>target</code> if not.</p> + +<pre class="brush: js">function OneClass() { + console.log('OneClass'); + console.log(new.target); +} +function OtherClass() { + console.log('OtherClass'); + console.log(new.target); +} + +var obj1 = Reflect.construct(OneClass, args); +// Output: +// OneClass +// function OneClass { ... } + +var obj2 = Reflect.construct(OneClass, args, OtherClass); +// Output: +// OneClass +// function OtherClass { ... } + +var obj3 = Object.create(OtherClass.prototype); +OneClass.apply(obj3, args); +// Output: +// OneClass +// undefined</pre> + +<p> </p> + +<h2 id="Examples">Examples</h2> + +<h3 id="Using_Reflect.construct()">Using <code>Reflect.construct()</code></h3> + +<pre class="brush: js">var d = Reflect.construct(Date, [1776, 6, 4]); +d instanceof Date; // true +d.getFullYear(); // 1776 +</pre> + +<h2 id="Specifications">Specifications</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('ES2015', '#sec-reflect.construct', 'Reflect.construct')}}</td> + <td>{{Spec2('ES2015')}}</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="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("javascript.builtins.Reflect.construct")}}</p> + +<h2 id="See_also">See also</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> |