Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Onze vrijwilligers hebben dit artikel nog niet naar het Nederlands vertaald. Doe mee en help de klus te klaren!

Obsolete
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

The Array.observe() method was used for asynchronously observing changes to Arrays, similar to Object.observe() for objects. It provided a stream of changes in order of occurrence. It's equivalent to Object.observe() invoked with the accept type list ["add", "update", "delete", "splice"]. However, this API has been deprecated and removed from Browsers. You can use the more general Proxy object instead.

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'], addedCount: 3}]

Specifications

Strawman proposal specification.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 36 [1] No support No support No support No support
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support No support (Yes) No support No support No support No support

[1] Deprecated in Chrome 49 and flagged off in Chrome 50 and entirely removed in Chrome 52

See also

Documentlabels en -medewerkers

 Aan deze pagina hebben bijgedragen: RandyOu, ziyunfei, fscholz, luciopaiva, MaxArt, DirectXMan12
 Laatst bijgewerkt door: RandyOu,