From a065e04d529da1d847b5062a12c46d916408bf32 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 21:46:22 -0500 Subject: update based on https://github.com/mdn/yari/issues/2028 --- .../global_objects/object/watch/index.html | 191 --------------------- 1 file changed, 191 deletions(-) delete mode 100644 files/zh-tw/web/javascript/reference/global_objects/object/watch/index.html (limited to 'files/zh-tw/web/javascript/reference') diff --git a/files/zh-tw/web/javascript/reference/global_objects/object/watch/index.html b/files/zh-tw/web/javascript/reference/global_objects/object/watch/index.html deleted file mode 100644 index 9dc8afa27f..0000000000 --- a/files/zh-tw/web/javascript/reference/global_objects/object/watch/index.html +++ /dev/null @@ -1,191 +0,0 @@ ---- -title: Object.prototype.watch() -slug: Web/JavaScript/Reference/Global_Objects/Object/watch -translation_of: Archive/Web/JavaScript/Object.watch ---- -
{{JSRef}}
- -
-

Warning: Generally you should avoid using watch() and {{jsxref("Object.prototype.unwatch", "unwatch()")}} when possible. These two methods are implemented only in Gecko, and they're intended primarily for debugging use. In addition, using watchpoints has a serious negative impact on performance, which is especially true when used on global objects, such as window. You can usually use setters and getters or proxies instead. See {{anch("Browser compatibility")}} for details. Also, do not confuse {{jsxref("Object.prototype.watch", "Object.watch")}} with {{jsxref("Object.prototype.observe", "Object.observe")}}.

-
- -

The watch() method watches for a property to be assigned a value and runs a function when that occurs.

- -

語法

- -
obj.watch(prop, handler)
- -

參數

- -
-
prop
-
所需要監聽其值是否改變的物件屬性
-
handler
-
當監聽的變數其數值變換時所執行的function
-
- -

回傳值

- -

{{jsxref("undefined")}}.

- -

Description

- -

Watches for assignment to a property named prop in this object, 呼叫 handler(prop, oldval, newval) whenever prop is set and storing the return value in that property. A watchpoint can filter (or nullify) the value assignment, by returning a modified newval (or by returning oldval).

- -

當你刪掉所監聽的物件屬性,並不會結束針對該物件屬性的監聽。當你重新產生該屬性時,監聽依舊維持作用。

- -

要停止該次監聽, 須使用 {{jsxref("Object.unwatch", "unwatch()")}} 函式. By default, the watch method is inherited by every object descended from {{jsxref("Object")}}.

- -

The JavaScript debugger has functionality similar to that provided by this method, as well as other debugging options. For information on the debugger, see Venkman.

- -

In Firefox, handler is only called from assignments in script, not from native code. For example, window.watch('location', myHandler) will not call myHandler if the user clicks a link to an anchor within the current document. However, window.location += '#myAnchor' will call myHandler.

- -
-

Note: Calling watch() on an object for a specific property overrides any previous handler attached for that property.

-
- -

範例

- -

使用watch 和 unwatch

- -
var o = { p: 1 };
-
-o.watch('p', function (id, oldval, newval) {
-  console.log('o.' + id + ' changed from ' + oldval + ' to ' + newval);
-  return newval;
-});
-
-o.p = 2;
-o.p = 3;
-delete o.p;
-o.p = 4;
-
-o.unwatch('p');
-o.p = 5;
-
- -

上述程式執行結果:

- -
o.p changed from 1 to 2
-o.p changed from 2 to 3
-o.p changed from undefined to 4
-
- -

使用 watch 驗證物件的屬性

- -

You can use watch to test any assignment to an object's properties. This example ensures that every Person always has a valid name and an age between 0 and 200.

- -
Person = function(name, age) {
-  this.watch('age', Person.prototype._isValidAssignment);
-  this.watch('name', Person.prototype._isValidAssignment);
-  this.name = name;
-  this.age = age;
-};
-
-Person.prototype.toString = function() {
-  return this.name + ', ' + this.age;
-};
-
-Person.prototype._isValidAssignment = function(id, oldval, newval) {
-  if (id === 'name' && (!newval || newval.length > 30)) {
-    throw new RangeError('invalid name for ' + this);
-  }
-  if (id === 'age'  && (newval < 0 || newval > 200)) {
-    throw new RangeError('invalid age for ' + this);
-  }
-  return newval;
-}
-
-will = new Person('Will', 29);
-console.log(will);   // Will, 29
-
-try {
-  will.name = '';
-} catch (e) {
-  console.log(e);
-}
-
-try {
-  will.age = -4;
-} catch (e) {
-  console.log(e);
-}
-
- -

上述程式執行結果:

- -
Will, 29
-RangeError: invalid name for Will, 29
-RangeError: invalid age for Will, 29
-
- -

規格

- -

Not part of any specifications. Implemented in JavaScript 1.2.

- -

瀏覽器相容性

- -
{{CompatibilityTable}}
- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatNo}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatNo}}{{CompatNo}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
- -

Compatibility notes

- - - -

參閱

- - -- cgit v1.2.3-54-g00ecf