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
190
191
|
---
title: Object.observe()
slug: Web/JavaScript/Reference/Global_Objects/Object/observe
translation_of: Archive/Web/JavaScript/Object.observe
---
<div>{{JSRef}}</div>
<p>El mètode <strong><code>Object.observe()</code></strong> s'utilitza per observar asincrònicament els canvis en un objecte. Proveeix una corrent de canvis en l'ordre en què es produeixen.</p>
<h2 id="Sintaxi">Sintaxi</h2>
<pre class="syntaxbox"><code>Object.observe(<var>obj</var>, <var>callback</var>[, <var>acceptList</var>])</code></pre>
<h3 id="Paràmetres">Paràmetres</h3>
<dl>
<dt><code>obj</code></dt>
<dd>L'objecte que s'observa.</dd>
<dt><code>callback</code></dt>
<dd>La funció es crida cada cop que es realitzen canvis, amb l'argument següent:
<dl>
<dt><code>changes</code></dt>
<dd>Un array d'objectes cadascún d'ells representa un canvi. Les propietats d'aquests objectes canvi són:
<ul>
<li><strong><code>name</code></strong>: El nom de la propietat que s'ha canviat.</li>
<li><strong><code>object</code></strong>: L'objecte canviat després d'haverse realitzat els canvis.</li>
<li><strong><code>type</code></strong>: Una cadena que indica el tipus de canvi que s'ha portat a terme: <code>"add"</code>, <code>"update"</code>, o <code>"delete"</code>.</li>
<li><strong><code>oldValue</code></strong>: Només pels tipus <code>"update"</code> i <code>"delete"</code>. El valor abans del canvi.</li>
</ul>
</dd>
</dl>
</dd>
<dt><code>acceptList</code></dt>
<dd>La llista de tipus de canvis que s'han d'observar en l'objecte donat callback. En cas d'ometre's, s'utilitzarà l'array <code>["add", "update", "delete", "reconfigure", "setPrototype", "preventExtensions"]</code>.</dd>
</dl>
<h2 id="Descripció">Descripció</h2>
<p><code>callback</code> és cridat cada cop que es realitza un canvi a <code>obj</code>, amb un array de tots els canvis en l'ordre en que han succeït.</p>
<h2 id="Exemples">Exemples</h2>
<h3 id="Mostrant_tots_els_sis_tipus_diferents">Mostrant tots els sis tipus diferents</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="Enllaçar_dades">Enllaçar dades</h3>
<pre class="brush: js">// Un model d'usuari
var user = {
id: 0,
name: 'Brendan Eich',
title: 'Mr.'
};
// Crear una benvinguda per l'usuari
function updateGreeting() {
user.greeting = 'Hello, ' + user.title + ' ' + user.name + '!';
}
updateGreeting();
Object.observe(user, function(changes) {
changes.forEach(function(change) {
// Qualsevol canvi de nom del temps o títol, actualitzar la benvinguda
if (change.name === 'name' || change.name === 'title') {
updateGreeting();
}
});
});
</pre>
<h3 id="Tipus_de_canvi_personalitzat">Tipus de canvi personalitzat</h3>
<pre class="brush: js">// Un punt en un pla 2D
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="Especificacions">Especificacions</h2>
<p><a href="https://github.com/arv/ecmascript-object-observe">Proposta de Strawman per ECMAScript 7</a>.</p>
<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Característica</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Suport bàsic</td>
<td>{{CompatChrome("36")}}</td>
<td>{{CompatNo}} [1]</td>
<td>{{CompatNo}} [2]</td>
<td>{{CompatOpera("23")}}</td>
<td>{{CompatNo}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Característica</th>
<th>Android</th>
<th>Chrome per Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Suport bàsic</td>
<td>{{CompatNo}}</td>
<td>{{CompatChrome("36")}}</td>
<td>{{CompatNo}} [1]</td>
<td>{{CompatNo}} [2]</td>
<td>{{CompatOpera("23")}}</td>
<td>{{CompatNo}}</td>
</tr>
</tbody>
</table>
</div>
<p>[1]: Vegeu {{bug(800355)}}</p>
<p>[2]: Vegeu rellevant <a href="https://dev.modern.ie/platform/status/objectobserve/">entrada de l'estat de la plataforma MS Edge</a></p>
<h2 id="Vegeu_també">Vegeu també</h2>
<ul>
<li>{{jsxref("Object.unobserve()")}} {{experimental_inline}}</li>
<li>{{jsxref("Array.observe()")}} {{experimental_inline}}</li>
</ul>
|