★ wanayoo — archive 1999 https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/observeNouvelle recherche | Portail wanayoo

Join MDN and developers like you at Mozilla's View Source conference, November 2-4 in Portland, Oregon. Learn more at https://viewsourceconf.org/.

Array.observe()

아직 자원 봉사자들이 한국어로 현재 문서를 번역하지 않았습니다. 가입해서 이 문서가 번역되는 일에 함께 해 주세요!

This is an experimental technology, part of the ECMAScript 2016 (ES7) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

The Array.observe() method is used for asynchronously observing changes to Arrays, similar to Object.observe() for objects. It provides a stream of changes in order of occurrence. It's equivalent to Object.observe() invoked with the accept type list ["add", "update", "delete", "splice"].

Syntax

Array.observe(arr, callback)

Parameters

arr
The array to be observed.
callback
The function called each time changes are made, with the following argument:
changes
An array of objects each representing a change. The properties of these change objects are:
  • name: The name of the property which was changed.
  • object: The changed array after the change was made.
  • type: A string indicating the type of change taking place. One of "add", "update", "delete", or "splice".
  • oldValue: Only for "update" and "delete" types. The value before the change.
  • index: Only for the "splice" type. The index at which the change occurred.
  • removed: Only for the "splice" type. An array of the removed elements.
  • addedCount: Only for the "splice" type. The number of elements added.

Description

The callback function is called each time a change is made to arr, with an array of all changes in the order in which they occurred.

Changes done via Array methods, such as Array.prototype.pop() will be reported as "splice" changes. Index assignment changes which do not change the length of the array may be reported as "update" changes.

Examples

Logging different change types

var arr = ['a', 'b', 'c'];

Array.observe(arr, function(changes) {
  console.log(changes);
});

arr[1] = 'B';
// [{type: 'update', object: <arr>, name: '1', oldValue: 'b'}]

arr[3] = 'd';
// [{type: 'splice', object: <arr>, index: 3, removed: [], addedCount: 1}]

arr.splice(1, 2, 'beta', 'gamma', 'delta');
// [{type: 'splice', object: <arr>, index: 1, removed: ['B', 'c', 'd'], addedCount: 3}]

Specifications

Strawman proposal for ECMAScript 7.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 36 Not supported Not supported Not supported Not supported
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support Not supported (Yes) Not supported Not supported Not supported Not supported

See also

문서 태그 및 공헌자

Contributors to this page: fscholz, luciopaiva, MaxArt, DirectXMan12
최종 변경: fscholz,
사이드바 숨기기