diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
| commit | da78a9e329e272dedb2400b79a3bdeebff387d47 (patch) | |
| tree | e6ef8aa7c43556f55ddfe031a01cf0a8fa271bfe /files/it/web/javascript/reference/global_objects/object/tostring | |
| parent | 1109132f09d75da9a28b649c7677bb6ce07c40c0 (diff) | |
| download | translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.gz translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.bz2 translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.zip | |
initial commit
Diffstat (limited to 'files/it/web/javascript/reference/global_objects/object/tostring')
| -rw-r--r-- | files/it/web/javascript/reference/global_objects/object/tostring/index.html | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/files/it/web/javascript/reference/global_objects/object/tostring/index.html b/files/it/web/javascript/reference/global_objects/object/tostring/index.html new file mode 100644 index 0000000000..5a77ea1a3e --- /dev/null +++ b/files/it/web/javascript/reference/global_objects/object/tostring/index.html @@ -0,0 +1,170 @@ +--- +title: Object.prototype.toString() +slug: Web/JavaScript/Reference/Global_Objects/Object/toString +tags: + - JavaScript + - Method + - Object + - Prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Object/toString +--- +<div>{{JSRef("Global_Objects", "Object")}}</div> + +<h2 id="Summary" name="Summary">Sommario</h2> + +<p>Il metodo <code><strong>toString()</strong></code> restituisce una stringa a che rappresenta l'oggetto.</p> +<div>{{EmbedInteractiveExample("pages/js/object-prototype-tostring.html")}}</div> + +<div class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div> +<h2 id="Syntax" name="Syntax">Sintassi</h2> + +<pre class="syntaxbox notranslate"><code><var>obj</var>.toString()</code></pre> + +<h2 id="Description" name="Description">Descrizione</h2> + +<p>Ogni oggetto ha un metodo <code>toString()</code> che è automaticamente chiamato quando l'oggetto deve essere rappresentato come valore testuale o quando l'oggetto è referenziato in un contesto in cui viene attesa una stringa. Di default, il metodo <code>toString()</code> è ereditato da ogni oggetto che discende da <code>Object</code>. Se il metodo non è sovrascritto in un oggetto personalizzato, <code>toString()</code> restituisce "[object <em>type</em>]", dove <code><em>type</em></code> è il tipo di oggetto. Il codice di seguito lo illustra:</p> + +<pre class="brush: js notranslate">var o = new Object(); +o.toString(); // returns [object Object] +</pre> + +<div class="note"> +<p><strong>Nota:</strong> A partire da JavaScript 1.8.5 <code>toString()</code> richiamato su {{jsxref("Global_Objects/null", "null")}} restituisce <code>[object <em>Null</em>]</code>, e {{jsxref("Global_Objects/undefined", "undefined")}} restituisce <code>[object <em>Undefined</em>]</code>, come definito nella versione 5 di ECMAScript e nei succcessivi Errata. Vedi {{anch("Example:_Using_toString_to_detect_object_type", "Using toString to detect object type")}}.</p> +</div> + +<h2 id="Examples" name="Examples">Esempi</h2> + +<h3 id="Example_Overriding_the_default_toString_method" name="Example:_Overriding_the_default_toString_method">Esempio: Sovrascrittura del metodo di default <code>toString</code> </h3> + +<p>Puoi creare una funzione che deve essere richiamata al posto del default metodo <code>toString()</code>. Il metodo <code>toString()</code> non prende argomenti e deve restituire una stringa. Esso può assumere qualunque valore tu voglia, ma sarà molto utile se comunichi informazioni sull'oggetto.</p> + +<p>Il codice seguente definisce l'oggetto <code>Dog</code> e crea <code>theDog</code>, ovvero un oggetto di tipo <code>Dog</code>:</p> + +<pre class="brush: js notranslate">function Dog(name, breed, color, sex) { + this.name = name; + this.breed = breed; + this.color = color; + this.sex = sex; +} + +theDog = new Dog('Gabby', 'Lab', 'chocolate', 'female'); +</pre> + +<p>Richiamando il metodo <code>toString()</code> su questo oggetto personalizzato, esso restituisce il valore di default ereditato da {{jsxref("Global_Objects/Object", "Object")}}:</p> + +<pre class="brush: js notranslate">theDog.toString(); // returns [object Object] +</pre> + +<p>Il codice seguente crea e assegna il metodo <code>dogToString()</code> per sovrascrivere il metodo di default <code>toString()</code>. Questa funzione genera una stringa contenente i valori name, breed, color e sex dell'oggetto, nella forma di "<code>property = value;</code>".</p> + +<pre class="brush: js notranslate">Dog.prototype.toString = function dogToString() { + var ret = 'Dog ' + this.name + ' is a ' + this.sex + ' ' + this.color + ' ' + this.breed; + return ret; +} +</pre> + +<p>Col precedente codice, la funzione <code>dogToString()</code> è richiamata automaticamente da JavaScript ogni volta che l'oggetto <code style="font-style: normal;">theDog</code> è usato in un contesto string, e restituisce la seguente stringa:</p> + +<pre class="notranslate">Dog Gabby is a female chocolate Lab +</pre> + +<h3 id="Example_Using_toString_to_detect_object_type" name="Example:_Using_toString_to_detect_object_type">Esempio: Uso di <code>toString()</code> per individuare l'oggetto class</h3> + +<p><code>toString()</code> può essere usato con ogni oggetto e permette di ottenere il suo class. Per usare <code>Object.prototype.toString()</code> con ogni oggetto, c'è bisogno di richiamare {{jsxref("Function.prototype.call()")}} o {{jsxref("Function.prototype.apply()")}} su di esso, passando l'oggetto che si cerca di ispezionare come primo parametro chiamato <code>thisArg</code>.</p> + +<pre class="brush: js notranslate">var toString = Object.prototype.toString; + +toString.call(new Date); // [object Date] +toString.call(new String); // [object String] +toString.call(Math); // [object Math] + +// Since JavaScript 1.8.5 +toString.call(undefined); // [object Undefined] +toString.call(null); // [object Null] +</pre> + +<h2 id="Specifiche">Specifiche</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specifiche</th> + <th scope="col">Stato</th> + <th scope="col">Commento</th> + </tr> + <tr> + <td>ECMAScript 1 Edizione.</td> + <td>Standard</td> + <td>Definizione iniziale. Implementato in JavaScript 1.0.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.2.4.2', 'Object.prototype.toString')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>Richiamato su {{jsxref("Global_Objects/null", "null")}} restituisce <code>[object <em>Null</em>]</code>, e {{jsxref("Global_Objects/undefined", "undefined")}} restituisce <code>[object <em>Undefined</em>]</code></td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-object.prototype.tostring', 'Object.prototype.toString')}}</td> + <td>{{Spec2('ES6')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilità_browser">Compatibilità browser</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Caratteristiche</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Support Base</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</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>Support Base</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also" name="See_also">Vedi anche</h2> + +<ul> + <li>{{jsxref("Object.prototype.toSource()")}}</li> + <li>{{jsxref("Object.prototype.valueOf()")}}</li> +</ul> |
