1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
---
title: Array.prototype.pop()
slug: Web/JavaScript/Reference/Global_Objects/Array/pop
tags:
- Array
- JavaScript
- Prototype
- Referenza
- metodo
translation_of: Web/JavaScript/Reference/Global_Objects/Array/pop
---
<div>{{JSRef}}</div>
<p>Il metodo <code><strong>pop()</strong></code> rimuove <strong>l'ultimo</strong> elemento da un array e restituisce quell'elemento. Questo metodo modifica la lunghezza dell'array.</p>
<div>{{EmbedInteractiveExample("pages/js/array-pop.html")}}</div>
<h2 id="Sintassi">Sintassi</h2>
<pre class="syntaxbox"><var>arr</var>.pop()</pre>
<h3 id="Valore_di_ritorno">Valore di ritorno</h3>
<p>L'elemento rimosso dall'array; {{jsxref("undefined")}} se l'array è vuoto.</p>
<h2 id="Descrizione">Descrizione</h2>
<p>Il metodo <code>pop</code> rimuove l'ultimo elemento da un array e restituisce quel valore al chiamante.</p>
<p><code>pop</code> è intenzionalmente generico; questo metodo può essere {{jsxref("Function.call", "chiamato", "", 1)}} o {{jsxref("Function.apply", "applicato", "", 1)}} ad oggetti che assomigliano agli array. Gli oggetti che non contengono una proprietà <code>length</code> che riflette l'ultimo di una serie di proprietà numeriche consecutive basate su zero potrebbero non comportarsi in alcun modo significativo.</p>
<p>Se chiami <code>pop()</code> su un array vuoto, ritorna {{jsxref("undefined")}}.</p>
<p>{{jsxref("Array.prototype.shift()")}} ha un comportamento simile a <code>pop</code>, ma applicato al primo elemento di un array.</p>
<h2 id="Esempi">Esempi</h2>
<h3 id="Rimozione_dell'ultimo_elemento_di_un_array">Rimozione dell'ultimo elemento di un array</h3>
<p>Il seguente codice crea l'array <code>myFish</code> contenente quattro elementi, dopo rimuove il suo ultimo elemento.</p>
<pre class="brush: js">var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
var popped = myFish.pop();
console.log(myFish); // ['angel', 'clown', 'mandarin' ]
console.log(popped); // 'sturgeon'</pre>
<h3 id="Usare_apply(_)_o_call_(_)_sugli_array-like_objects">Usare apply( ) o call ( ) sugli array-like objects</h3>
<p>Il codice seguente crea l'array-like object <code>myFish</code> contenente quattro elementi e un parametro length, poi rimuove il suo ultimo elemento e decrementa il parametro length.</p>
<pre class="brush: js">var myFish = {0:'angel', 1:'clown', 2:'mandarin', 3:'sturgeon', length: 4};
var popped = Array.prototype.pop.call(myFish); //same syntax for using apply( )
console.log(myFish); // {0:'angel', 1:'clown', 2:'mandarin', length: 3}
console.log(popped); // 'sturgeon'
</pre>
<h2 id="Specifiche">Specifiche</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specifica</th>
<th scope="col">Stato</th>
<th scope="col">Commento</th>
</tr>
<tr>
<td>{{SpecName('ES3')}}</td>
<td>{{Spec2('ES3')}}</td>
<td>Definizione iniziale Implementato in JavaScript 1.2.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.4.4.6', 'Array.prototype.pop')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-array.prototype.pop', 'Array.prototype.pop')}}</td>
<td>{{Spec2('ES6')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-array.prototype.pop', 'Array.prototype.pop')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="Compatibilità_con_i_browser">Compatibilità con i browser</h2>
<div>
<p>{{Compat("javascript.builtins.Array.pop")}}</p>
</div>
<h2 id="Vedi_anche">Vedi anche</h2>
<ul>
<li>{{jsxref("Array.prototype.push()")}}</li>
<li>{{jsxref("Array.prototype.shift()")}}</li>
<li>{{jsxref("Array.prototype.unshift()")}}</li>
<li>{{jsxref("Array.prototype.concat()")}}</li>
<li>{{jsxref("Array.prototype.splice()")}}</li>
</ul>
|