--- title: Array.unobserve() slug: Web/JavaScript/Reference/Global_Objects/Array/unobserve translation_of: Archive/Web/JavaScript/Array.unobserve ---
Array.unobserve()方法用来移除{{jsxref("Array.observe()")}}设置的所有观察者。
Array.unobserve(arr, callback)
arr
callback回调
Array.unobserve()
方法因为要移除观察者,所以应该在{{jsxref("Array.observe()")}}调用后调用。
回调函数应该是一个函数的引用并且不能是匿名函数, 因为这个函数需要用来移除前面的观察者, 如果用匿名函数是没有用的,将不能移除任何观察者。
var arr = [1, 2, 3]; var observer = function(changes) { console.log(changes); } Array.observe(arr, observer); arr.push(4); // [{type: "splice", object: <arr>, index: 3, removed:[], addedCount: 1}] Array.unobserve(arr, observer); arr.pop(); // The callback wasn't called
var persons = ['Khalid', 'Ahmed', 'Mohammed']; Array.observe(persons, function (changes) { console.log(changes); }); persons.shift(); // [{type: "splice", object: <arr>, index: 0, removed: [ "Khalid" ], addedCount: 0 }] Array.unobserve(persons, function (changes) { console.log(changes); }); persons.push('Abdullah'); // [{type: "splice", object: <arr>, index: 2, removed: [], addedCount: 1 }] // The callback will always be called
{{Compat("javascript.builtins.Array.unobserve")}}