Object.prototype.watch()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/watch
  • Revision title: watch
  • Revision id: 49857
  • Created:
  • Creator: Trinithis
  • Is current revision? No
  • Comment /* Example: Using <code>watch</code> to validate an object's properties */

Revision Content

{{wiki.template('Non-standard_header')}}

Summary

Watches for a property to be assigned a value and runs a function when that occurs.

Method of Object
Implemented in: JavaScript 1.2, NES 3.0

Syntax

watch(prop, handler)

Parameters

prop 
The name of a property of the object.
handler 
A function to call.

Description

Watches for assignment to a property named prop in this object, calling 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).

If you delete a property for which a watchpoint has been set, that watchpoint does not disappear. If you later recreate the property, the watchpoint is still in effect.

To remove a watchpoint, use the unwatch method. By default, the watch method is inherited by every object descended from 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 NES 3.0 and 4.x, handler is called from assignments in script as well as native code. 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, the following code will call myHandler:

window.location += '#myAnchor';

Examples

Example: Using watch and unwatch

<script type="text/javascript">

var o = {p:1};
o.watch("p",
   function (id,oldval,newval) {
      document.writeln("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;

</script>

This script displays the following:

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

Example: Using watch to validate an object's properties

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.

<script type="text/javascript">

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); // --> Will,29
document.writeln(will);

try {
  will.name="";  // --> Error "invalid name for Will,29"
} catch (e) { document.writeln(e); }

try {
  will.age=-4;   // --> Error "invalid age  for Will,29"
} catch (e) { document.writeln(e); }

</script>

This script displays the following:

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

See Also

unwatch


{{ wiki.languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/Object/watch", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/watch", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/watch" } ) }}

Revision Source

<p>
{{wiki.template('Non-standard_header')}}
</p>
<h3 name="Summary"> Summary </h3>
<p>Watches for a property to be assigned a value and runs a function when that occurs.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Method of <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object">Object</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.2, NES 3.0</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code>
watch(<i>prop</i>, <i>handler</i>)
</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>prop</code> </dt><dd> The name of a property of the object.
</dd></dl>
<dl><dt> <code>handler</code> </dt><dd> A function to call.
</dd></dl>
<h3 name="Description"> Description </h3>
<p>Watches for assignment to a property named <code>prop</code> in this object, calling <code>handler(prop, oldval, newval)</code> whenever <code>prop</code> is set and storing the return value in that property. A watchpoint can filter (or nullify) the value assignment, by returning a modified <code>newval</code> (or by returning <code>oldval</code>).
</p><p>If you delete a property for which a watchpoint has been set, that watchpoint does not disappear. If you later recreate the property, the watchpoint is still in effect.
</p><p>To remove a watchpoint, use the <code><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/unwatch">unwatch</a></code> method. By default, the <code>watch</code> method is inherited by every object descended from <code>Object</code>.
</p><p>The JavaScript debugger has functionality similar to that provided by this method, as well as other debugging options. For information on the debugger, see <a href="en/Venkman">Venkman</a>.
</p><p>In NES 3.0 and 4.x, <code>handler</code> is called from assignments in script as well as native code.  In Firefox, <code>handler</code> is only called from assignments in script, not from native code.  For example, <code>window.watch('location', myHandler)</code> will not call <code>myHandler</code> if the user clicks a link to an anchor within the current document.  However, the following code will call <code>myHandler</code>:
</p>
<pre class="eval"><code>window.location += '#myAnchor';</code>
</pre>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Using_watch_and_unwatch"> Example: Using <code>watch</code> and <code>unwatch</code> </h4>
<pre>&lt;script type="text/javascript"&gt;

var o = {p:1};
o.watch("p",
   function (id,oldval,newval) {
      document.writeln("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;

&lt;/script&gt;
</pre>
<p>This script displays the following:
</p>
<pre>o.p changed from 1 to 2
o.p changed from 2 to 3
o.p changed from undefined to 4
</pre>
<h4 name="Example:_Using_watch_to_validate_an_object.27s_properties"> Example: Using <code>watch</code> to validate an object's properties </h4>
<p>You can use <code>watch</code> 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.
</p>
<pre>&lt;script type="text/javascript"&gt;

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" &amp;&amp; (!newval || newval.length&gt;30)) { throw new RangeError("invalid name for "+this); }
  if (id=="age"  &amp;&amp; (newval&lt;0 || newval&gt;200))      { throw new RangeError("invalid age  for "+this ); }
  return newval;
};

will = new Person("Will",29); // --&gt; Will,29
document.writeln(will);

try {
  will.name="";  // --&gt; Error "invalid name for Will,29"
} catch (e) { document.writeln(e); }

try {
  will.age=-4;   // --&gt; Error "invalid age  for Will,29"
} catch (e) { document.writeln(e); }

&lt;/script&gt;

</pre>
<p>This script displays the following:
</p>
<pre>Will,29
RangeError: invalid name for Will,29
RangeError: invalid age  for Will,29
</pre>
<h3 name="See_Also"> See Also </h3>
<p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/unwatch">unwatch</a>
</p><p><br>
</p>
<div class="noinclude">
</div>
{{ wiki.languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/Object/watch", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/watch", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/watch" } ) }}
Revert to this revision