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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
---
title: Object.observe()
slug: Web/JavaScript/Reference/Global_Objects/Object/observe
translation_of: Archive/Web/JavaScript/Object.observe
---
<div>{{JSRef}} {{obsolete_header}}</div>
<h2 id="Sommario">Sommario</h2>
<p>Il metodo <strong><code>Object.observe()</code></strong> è usato per l'osservazione asincrona dei cambiamenti di un oggetto. Esso fornisce uno stream dei cambiamenti nell'ordine in cui si verificano.</p>
<h2 id="Sintassi">Sintassi</h2>
<pre class="syntaxbox"><code>Object.observe(<var>obj</var>, <var>callback</var>[, <var>acceptList</var>])</code></pre>
<h3 id="Parametri">Parametri</h3>
<dl>
<dt><code>obj</code></dt>
<dd>L'oggetto che verrà osservato.</dd>
<dt><code>callback</code></dt>
<dd>La funzione richiamata ogni volta che si verificano delle modifiche, con i seguenti argomenti:
<dl>
<dt><code>changes</code></dt>
<dd>Un array di oggetti di oggetti che rappresentano una modifica. Le properties di questi oggetti sono:
<ul>
<li><strong><code>name</code></strong>: Il nome della property che è stata modificata.</li>
<li><strong><code>object</code></strong>: L'oggetto modificato dopo che la modifica è avvenuta.</li>
<li><strong><code>type</code></strong>: Una stringa che indica il tipo di modifica in atto. Può essere valorizzata con <code>"add"</code>, <code>"update"</code> o <code>"delete"</code>.</li>
<li><strong><code>oldValue</code></strong>: Solo per i tipi <code>"update"</code> e <code>"delete"</code>. Indica il valore prima della modifica.</li>
</ul>
</dd>
</dl>
</dd>
<dt><code>acceptList</code></dt>
<dd>La lista dei tipi di modifiche che possono essere osservate su un dato oggetto per un dato callback. Se omesso, sarà usato l'array <code>["add", "update", "delete", "reconfigure", "setPrototype", "preventExtensions"]</code>.</dd>
</dl>
<h2 id="Descrizione">Descrizione</h2>
<p>La funzione <code>callback</code> è chiamata ogni volta che una modifica viene fatta sull'<span style="font-family: Consolas,Monaco,'Andale Mono',monospace;">obj.</span> Ad essa viene passata un'array di tutte le modifiche, nell'ordine in cui si verificano.</p>
<h2 id="Esempi">Esempi</h2>
<h3 id="Esempio_Log_di_tutti_e_sei_differenti_tipi">Esempio: Log di tutti e sei differenti tipi</h3>
<pre class="brush: js">var obj = {
foo: 0,
bar: 1
};
Object.observe(obj, function(changes) {
console.log(changes);
});
obj.baz = 2;
// [{name: 'baz', object: <obj>, type: 'add'}]
obj.foo = 'hello';
// [{name: 'foo', object: <obj>, type: 'update', oldValue: 0}]
delete obj.baz;
// [{name: 'baz', object: <obj>, type: 'delete', oldValue: 2}]
Object.defineProperty(obj, 'foo', {writable: false});
// [{name: 'foo', object: <obj>, type: 'reconfigure'}]
Object.setPrototypeOf(obj, {});
// [{name: '__proto__', object: <obj>, type: 'setPrototype', oldValue: <prototype>}]
Object.seal(obj);
// [
// {name: 'foo', object: <obj>, type: 'reconfigure'},
// {name: 'bar', object: <obj>, type: 'reconfigure'},
// {object: <obj>, type: 'preventExtensions'}
// ]
</pre>
<h3 id="Esempio_Data_Binding">Esempio: Data Binding</h3>
<pre class="brush: js">// A user model
var user = {
id: 0,
name: 'Brendan Eich',
title: 'Mr.'
};
// Create a greeting for the user
function updateGreeting() {
user.greeting = 'Hello, ' + user.title + ' ' + user.name + '!';
}
updateGreeting();
Object.observe(user, function(changes) {
changes.forEach(function(change) {
// Any time name or title change, update the greeting
if (change.name === 'name' || change.name === 'title') {
updateGreeting();
}
});
});
</pre>
<h3 id="Esempio_Tipo_di_modifica_personalizzata">Esempio: Tipo di modifica personalizzata</h3>
<pre class="brush: js">// A point on a 2D plane
var point = {x: 0, y: 0, distance: 0};
function setPosition(pt, x, y) {
// Performing a custom change
Object.getNotifier(pt).performChange('reposition', function() {
var oldDistance = pt.distance;
pt.x = x;
pt.y = y;
pt.distance = Math.sqrt(x * x + y * y);
return {oldDistance: oldDistance};
});
}
Object.observe(point, function(changes) {
console.log('Distance change: ' + (point.distance - changes[0].oldDistance));
}, ['reposition']);
setPosition(point, 3, 4);
// Distance change: 5
</pre>
<h2 id="Specifications" name="Specifications">Specifiche</h2>
<p><a href="https://github.com/arv/ecmascript-object-observe">Argomentazione proposta per ECMAScript 7</a>.</p>
<h2 id="Browser_compatibility" name="Browser_compatibility">Compatibilita browser</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Caratteristica</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Supporto base</td>
<td>{{CompatChrome("36")}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatOpera("23")}}</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>Supporto base</td>
<td>{{CompatNo}}</td>
<td>{{CompatChrome("36")}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatOpera("23")}}</td>
<td>{{CompatNo}}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="See_also" name="See_also">Vedi anche</h2>
<ul>
<li>{{jsxref("Object.unobserve()")}} {{experimental_inline}}</li>
<li>{{jsxref("Array.observe()")}} {{experimental_inline}}</li>
</ul>
|