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:
- 감시 객체
translation_of: Archive/Web/JavaScript/Object.observe
---
<div>{{JSRef}}</div>
<p><strong><code>Object.observe()</code></strong> 메소드는 객체의 변화를 비동기로 감시하는데에 사용된다. 이 메소드는 변화들이 발생한 순서대로 그 흐름을 제공한다.</p>
<h2 id="문법">문법</h2>
<pre class="syntaxbox"><code>Object.observe(<var>obj</var>, <var>callback</var>[, <var>acceptList</var>])</code></pre>
<h3 id="파라미터">파라미터</h3>
<dl>
<dt><code>obj</code></dt>
<dd>감시될 객체입니다.</dd>
<dt><code>callback</code></dt>
<dd><code>obj</code>의 변경이 일어났을 때마다 호출될 함수입니다. 다음과 같은 인자를 갖습니다.
<dl>
<dt><code>changes</code></dt>
<dd>변경 사항을 나타내는 객체의 배열입니다. 그 객체의 프로퍼티는 다음과 같습니다.
<ul>
<li><strong><code>name</code></strong>: 변경된 프로퍼티의 이름입니다.</li>
<li><strong><code>object</code></strong>: 변경이 일어난 뒤의 객체입니다.</li>
<li><strong><code>type</code></strong>: 변경의 종류를 의미하는 string입니다. <code>"add"</code>, <code>"update"</code>, <code>"delete"</code> 중 하나입니다.</li>
<li><strong><code>oldValue</code></strong>: 변경되기 이전의 값입니다. <code>"update"와</code> <code>"delete"</code> 타입에만 존재합니다.</li>
</ul>
</dd>
</dl>
</dd>
<dt><code>acceptList</code></dt>
<dd>감시할 변경의 종류를 의미하는 리스트입니다. 주어지지 않은 경우, 배열 <code>["add", "update", "delete", "reconfigure", "setPrototype", "preventExtensions"]</code> 이 이용될 것입니다.</dd>
</dl>
<h2 id="설명">설명</h2>
<p><code>callback</code>은 <code>obj</code>에 변경이 있을 때마다 실행되며, 모든 변경 사항이 일어난 순서대로 담긴 배열이 전달됩니다.</p>
<h2 id="예제">예제</h2>
<h3 id="Logging_all_six_different_types">Logging all six different types</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="데이터_바인딩">데이터 바인딩</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="Custom_change_type">Custom change type</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">Specifications</h2>
<p><a href="https://github.com/arv/ecmascript-object-observe">Strawman proposal for ECMAScript 7</a>.</p>
<h2 id="브라우저_호환성">브라우저 호환성</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</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>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>Basic support</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]: See {{bug(800355)}}</p>
<p>[2]: See relevant <a href="https://dev.modern.ie/platform/status/objectobserve/">MS Edge platform status entry</a></p>
<h2 id="같이_보기">같이 보기</h2>
<ul>
<li>{{jsxref("Object.unobserve()")}} {{experimental_inline}}</li>
<li>{{jsxref("Array.observe()")}} {{experimental_inline}}</li>
</ul>
|