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
192
193
|
---
title: Object.observe()
slug: Web/JavaScript/Reference/Global_Objects/Object/observe
tags:
- ECMAScript7
- Experimental
- JavaScript
- Method
- Object
translation_of: Archive/Web/JavaScript/Object.observe
---
<div>{{JSRef}}</div>
<p><strong><code>Object.observe()</code></strong> được sử dụng để đồng bộ quan sát những thay đổi cho một đối tượng. Nó cung cấp một dòng thay đổi trong thứ tự mà chúng xảy ra.</p>
<h2 id="Cú_pháp">Cú pháp</h2>
<pre class="syntaxbox"><code>Object.observe(<var>obj</var>, <var>callback</var>[, <var>acceptList</var>])</code></pre>
<h3 id="Tham_số">Tham số</h3>
<dl>
<dt><code>obj</code></dt>
<dd>Đối tượng được quan sát.</dd>
<dt><code>callback</code></dt>
<dd>Hàm được gọi mỗi lần có thay đổi trong <code>obj</code>, với những tham số sau:
<dl>
<dt><code>changes</code></dt>
<dd>Một mảng các đối tượng mô tả sự thay đổi. Mỗi đối tượng chứa các thuộc tính:
<ul>
<li><strong><code>name</code></strong>: Tên thuộc tính đã thay đổi.</li>
<li><strong><code>object</code></strong>: Đối tượng được thay đổi.</li>
<li><strong><code>type</code></strong>: Một chuỗi cho biết loại thay đổi đang diễn ra. Có thể là <code>"add"</code>, <code>"update"</code>, hoặc <code>"delete"</code>.</li>
<li><strong><code>oldValue</code></strong>: Chỉ có cho loại <code>"update"</code> và <code>"delete"</code>. Cho biết giá trị trước khi thay đổi.</li>
</ul>
</dd>
</dl>
</dd>
<dt><code>acceptList</code></dt>
<dd>Danh sách các loại thay đổi được quan sát. Nếu bỏ qua, mặc định là <code>["add", "update", "delete", "reconfigure", "setPrototype", "preventExtensions"]</code>.</dd>
</dl>
<h2 id="Mô_tả">Mô tả</h2>
<p>Hàm<code> callback</code> được gọi mỗi khi có thay đổi trong <code>obj</code>, với một mảng các đối tượng chứa thông tin về sự thay đổi.</p>
<h2 id="Ví_dụ">Ví dụ</h2>
<h3 id="Ghi_lại_tất_cả_sáu_loại_thay_đổi_khác_nhau">Ghi lại tất cả sáu loại thay đổi khác nhau</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="Ràng_buộc_dữ_liệu">Ràng buộc dữ liệu</h3>
<pre class="brush: js">// Mô hình user
var user = {
id: 0,
name: 'Brendan Eich',
title: 'Mr.'
};
// Lời chào tới user
function updateGreeting() {
user.greeting = 'Hello, ' + user.title + ' ' + user.name + '!';
}
updateGreeting();
Object.observe(user, function(changes) {
changes.forEach(function(change) {
// Bất kỳ khi nào tên hoặc danh hiệu thay đổi, gọi updateGreeting()
if (change.name === 'name' || change.name === 'title') {
updateGreeting();
}
});
});
</pre>
<h3 id="Loại_thay_đổi_tùy_chỉnh">Loại thay đổi tùy chỉnh</h3>
<pre class="brush: js">// Một điểm trên một mặt phẳng
var point = {x: 0, y: 0, distance: 0};
function setPosition(pt, x, y) {
// Thực hiện thay đổi tùy chỉnh
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="Đặc_tả">Đặc tả</h2>
<p><a href="https://github.com/arv/ecmascript-object-observe">Strawman proposal for ECMAScript 7</a>.</p>
<h2 id="Khả_năng_tương_thích_trình_duyệt">Khả năng tương thích trình duyệt</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Tính năng</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Hỗ trợ cơ bản</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>Tính năng</th>
<th>Android</th>
<th>Chrome dành cho Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Hỗ trợ cơ bản</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="Xem_thêm">Xem thêm</h2>
<ul>
<li>{{jsxref("Object.unobserve()")}} {{experimental_inline}}</li>
<li>{{jsxref("Array.observe()")}} {{experimental_inline}}</li>
</ul>
|