diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-11 19:00:14 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-11 19:00:14 -0500 |
commit | ba5d6f9610d6bb352eecfa3ded1bb99bc9892916 (patch) | |
tree | 4ca1fd3d01433b96fce40c473a3b6b272be393eb /files/it | |
parent | d192fb918b0e2aa8869de6dcc59de8464b6e879a (diff) | |
download | translated-content-ba5d6f9610d6bb352eecfa3ded1bb99bc9892916.tar.gz translated-content-ba5d6f9610d6bb352eecfa3ded1bb99bc9892916.tar.bz2 translated-content-ba5d6f9610d6bb352eecfa3ded1bb99bc9892916.zip |
dump 2020-12-11
Diffstat (limited to 'files/it')
-rw-r--r-- | files/it/web/javascript/reference/global_objects/object/constructor/index.html | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/files/it/web/javascript/reference/global_objects/object/constructor/index.html b/files/it/web/javascript/reference/global_objects/object/constructor/index.html new file mode 100644 index 0000000000..6a9c339acb --- /dev/null +++ b/files/it/web/javascript/reference/global_objects/object/constructor/index.html @@ -0,0 +1,222 @@ +--- +title: Object.prototype.constructor +slug: Web/JavaScript/Reference/Global_Objects/Object/constructor +translation_of: Web/JavaScript/Reference/Global_Objects/Object/constructor +--- +<div>{{JSRef}}</div> + +<p>La proprietà constuctor restituisce un riferimento alla funzione del costruttore {{jsxref ("Object")}} che ha creato l'oggetto istanza. Notare che il valore di questa proprietà è un riferimento alla funzione stessa, non una stringa contenente il nome della funzione.Il valore è di sola lettura solo per i valori primitivi come 1, true e "test".</p> + +<pre dir="ltr" id="tw-target-rmn"></pre> + + + +<h2 id="Description">Description</h2> + +<pre dir="ltr" id="tw-target-text">Tutti gli oggetti (ad eccezione degli object.create (null)) avranno una proprietà constuctor. Gli oggetti creati senza l'uso esplicito di una funzione di constructor (come oggetti letterali e array-letterali) avranno una proprietà constructor che punta al tipo di costrunctor dell'oggetto fondamentale per quell'oggetto.</pre> + +<pre class="brush: js notranslate">let o = {} +o.constructor === Object // true + +let o = new Object +o.constructor === Object // true + +let a = [] +a.constructor === Array // true + +let a = new Array +a.constructor === Array // true + +let n = new Number(3) +n.constructor === Number // true +</pre> + +<h2 id="Examples">Examples</h2> + +<h3 id="Displaying_the_constructor_of_an_object">Displaying the constructor of an object</h3> + +<p>The following example creates a constructor (<code>Tree</code>) and an object of that type (<code>theTree</code>). The example then displays the <code>constructor</code> property for the object <code>theTree</code>.</p> + +<pre class="brush: js notranslate">function Tree(name) { + this.name = name +} + +let theTree = new Tree('Redwood') +console.log('theTree.constructor is ' + theTree.constructor) +</pre> + +<p>This example displays the following output:</p> + +<pre class="brush: js notranslate">theTree.constructor is function Tree(name) { + this.name = name +} +</pre> + +<h3 id="Changing_the_constructor_of_an_object">Changing the constructor of an object</h3> + +<p>One can assign the <code>constructor</code> property for any value except <code>null</code> and <code>undefined</code> since those don't have a corresponding constructor function (like <code>String</code>, <code>Number</code>, <code>Boolean</code> etc.), but values which are primitives won't keep the change (with no exception thrown). This is due to the same mechanism, which allows one to set any property on primitive values (except <code>null</code> and <code>undefined</code>) with no effect. namely wherenever one uses such a primitive as an object an instance of the corresponding constructor is created and discarded right after the statement was executed.</p> + +<pre class="brush: js notranslate">let val = null; +val.constructor = 1; //<span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox-stackTrace reps-custom-format">TypeError: <span class="objectBox objectBox-string">var is null</span></span></span></span></span> + +<span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox-stackTrace reps-custom-format"><span class="objectBox objectBox-string">val = 'abc';</span></span></span></span></span> +<span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox-stackTrace reps-custom-format"><span class="objectBox objectBox-string">val.constructor = Number; //val.constructor === String</span></span></span></span></span> + +<span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox-stackTrace reps-custom-format"><span class="objectBox objectBox-string">val.foo = 'bar';</span></span></span></span></span> //An implicit instance of <span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox-stackTrace reps-custom-format"><span class="objectBox objectBox-string">String('abc') was created and assigned the prop foo</span></span></span></span></span> +<span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox-stackTrace reps-custom-format"><span class="objectBox objectBox-string">val.foo === undefined; //true, since a new instance of String('abc') was created for this comparison, which doesn't have the foo property</span></span></span></span></span></pre> + +<p>So basically one can change the value of the <code>constructor</code> property for anything, except the primitives mentioned above, <strong>note that changing the </strong><code>constructor</code><strong> property does not affect the instanceof operator</strong>:</p> + +<pre class="brush: js notranslate">let a = []; +a.constructor = String +a.constructor === String // true +a instanceof String //false +a instanceof Array //true + +a = new Foo(); +a.constructor = 'bar' +a.constructor === 'bar' // true + +//etc.</pre> + +<p>If the object is sealed/frozen then the change has no effect and no exception is thrown:</p> + +<pre class="brush: js notranslate">let a = Object.seal({}); +a.constructor = Number; +a.constructor === Object; //true</pre> + +<h3 id="Changing_the_constructor_of_a_function">Changing the constructor of a function</h3> + +<p>Mostly this property is used for defining a function as a <strong>function-constructor</strong> with further calling it with <strong>new</strong> and prototype-inherits chain.</p> + +<pre class="brush: js notranslate">function Parent() { /* ... */ } +Parent.prototype.parentMethod = function parentMethod() {} + +function Child() { + Parent.call(this) // Make sure everything is initialized properly +} +Child.prototype = Object.create(Parent.prototype) // re-define child prototype to Parent prototype + +Child.prototype.constructor = Child // return original constructor to Child</pre> + +<p>But when do we need to perform the last line here? Unfortunately, the answer is: <em>it depends</em>.</p> + +<p>Let's try to define the cases in which re-assignment of the original constructor will play a major role, and when it will be one superfluous line of code.</p> + +<p>Take the following case: the object has the <code>create()</code> method to create itself.</p> + +<pre class="brush: js notranslate">function Parent() { /* ... */ } +function CreatedConstructor() { + Parent.call(this) +} + +CreatedConstructor.prototype = Object.create(Parent.prototype) + +CreatedConstructor.prototype.create = function create() { + return new this.constructor() +} + +new CreatedConstructor().create().create() // TypeError undefined is not a function since constructor === Parent</pre> + +<p>In the example above the exception will be shown since the constructor links to Parent.</p> + +<p>To avoid this, just assign the necessary constructor you are going to use.</p> + +<pre class="brush: js notranslate">function Parent() { /* ... */ } +function CreatedConstructor() { /* ... */ } + +CreatedConstructor.prototype = Object.create(Parent.prototype) +CreatedConstructor.prototype.constructor = CreatedConstructor // sets the correct constructor for future use + +CreatedConstructor.prototype.create = function create() { + return new this.constructor() +} + +new CreatedConstructor().create().create() // it's pretty fine</pre> + +<p>Ok, now it's pretty clear why changing the constructor can be useful.</p> + +<p>Let's consider one more case.</p> + +<pre class="brush: js notranslate">function ParentWithStatic() {} + +ParentWithStatic.startPosition = { x: 0, y:0 } // Static member property +ParentWithStatic.getStartPosition = function getStartPosition() { + return this.startPosition +} + +function Child(x, y) { + this.position = { + x: x, + y: y + } +} + +Child = Object.assign(ParentWithStatic) +Child.prototype = Object.create(ParentWithStatic.prototype) +Child.prototype.constructor = Child + +Child.prototype.getOffsetByInitialPosition = function getOffsetByInitialPosition() { + let position = this.position + let startPosition = this.constructor.getStartPosition() // error undefined is not a function, since the constructor is Child + + return { + offsetX: startPosition.x - position.x, + offsetY: startPosition.y - position.y + } +};</pre> + +<p>For this example we need either to stay parent constructor to continue to work properly or reassign static properties to child's constructor:</p> + +<pre class="brush: js notranslate">... +Child = Object.assign(ParentWithStatic) // Notice that we assign it before we create(...) a prototype below +Child.prototype = Object.create(ParentWithStatic.prototype) +... +</pre> + +<p>or assign parent constructor identifier to a separate property on the Child constructor function and access it via that property:</p> + +<pre class="brush: js notranslate">... +Child.parentConstructor = ParentWithStatic +Child.prototype = Object.create(ParentWithStatic.prototype) +... + let startPosition = this.constructor.parentConstructor.getStartPosition() +... +</pre> + +<div class="blockIndicator note"> +<p><strong>Summary</strong>: Manually updating or setting the constructor can lead to differrent and sometimes confusing consequences. To prevent this, just define the role of <code>constructor</code> in each specific case. In most cases, <code>constructor</code> is not used and reassignment of it is not necessary.</p> +</div> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-object.prototype.constructor', 'Object.prototype.constructor')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("javascript.builtins.Object.constructor")}}</p> + +<h2 id="See_also">See also</h2> + +<div class="hidden"> +<p>The curly braces here invoke standard macroses defined by the MDN wiki. Checkout here for more info: <a href="/en-US/docs/MDN/Contribute/Structures/Macros/Commonly-used_macros">https://developer.mozilla.org/en-US/docs/MDN/Contribute/Structures/Macros/Commonly-used_macros</a></p> +</div> + +<ul> + <li>{{jsxref("statements/class","Class declaration","",1)}}</li> + <li>{{jsxref("Classes/constructor","Class constructor","",1)}}</li> + <li>Glossary: {{Glossary("constructor", "", 1)}}</li> +</ul> |