diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/array/unobserve/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/array/unobserve/index.html | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/unobserve/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/unobserve/index.html new file mode 100644 index 0000000000..90678a1081 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/array/unobserve/index.html @@ -0,0 +1,86 @@ +--- +title: Array.unobserve() +slug: Web/JavaScript/Reference/Global_Objects/Array/unobserve +translation_of: Archive/Web/JavaScript/Array.unobserve +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p>Array<strong>.unobserve()方法用来移除</strong>{{jsxref("Array.observe()")}}设置的所有观察者。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code>Array.unobserve(<var>arr</var>, <var>callback</var>)</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>arr</code></dt> + <dd>停止观察的数组。</dd> + <dt> </dt> + <dt><code>callback回调</code></dt> + <dd>需要停止的array <strong>arr</strong>每次改变都会调用的函数引用。</dd> +</dl> + +<h2 id="描述">描述</h2> + +<p><code>Array.unobserve()</code> 方法因为要移除观察者,所以应该在{{jsxref("Array.observe()")}}调用后调用。</p> + +<p>回调函数应该是一个函数的引用并且不能是匿名函数, 因为这个函数需要用来移除前面的观察者, 如果用匿名函数是没有用的,将不能移除任何观察者。</p> + +<h2 id="例子">例子</h2> + +<h3 id="停止观察一个数组">停止观察一个数组</h3> + +<pre class="brush: js">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</pre> + +<h3 id="使用匿名函数">使用匿名函数</h3> + +<pre class="brush: js">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 +</pre> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> +<div> + + +<p>{{Compat("javascript.builtins.Array.unobserve")}}</p> +</div> +</div> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li>{{jsxref("Array.observe()")}} {{non-standard_inline}}</li> + <li>{{jsxref("Object.observe()")}} {{non-standard_inline}}</li> + <li>{{jsxref("Object.unobserve()")}} {{non-standard_inline}}</li> +</ul> |