watch

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

Applies to

Object

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 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.

Example

o = {p:1}
o.watch("p",
      function (id,oldval,newval) {
            Console.Write("o." + id + " changed from " 
                  + oldval + " to " + newval)
            return newval
      })

o.p = 2
o.p = 
delete o.p

o.p = 4

o.unwatch('p')

o.p = 5

This script displays the following:

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