JavaScript » Object » watch

Syntax:, handlerfunction)

This method adds a watchpoint to a property of the object.

The watch method is inherited by every object descended from Object and adds a watchpoint to a property of the method. Whenever a value is assigned to it, it calls up a function allowing you to watch any new value assigned and, if necessary, alter it.

A watchpoint for a property does not disappear if that property is deleted. It can, however, be removed by using the unwatch method.


city = {name:"Chicago"}"name", myfunction (property, oldval, newval)
                                       if(newval == "Leningrad")
                                          newval = "St. Petersburg"
                                       return newval
                ) //end of watch method

This following code watches the 'name' property of the 'city' object, and if the name 'Leningrad' is assigned to it, it is altered to the city's new name of 'St. Petersburg'. Note the code that is enclosed in the pair of curly braces (an if statement) which is associated with the handlerfunction argument called 'myfunction':