You should know that in C #, it is very easy to monitor changes to attributes and files, because there are good stuff support such as delegate (event) and filesystemwatcher.
In JavaScript, how does one monitor variable changes? First, I followed the C # Attribute to operate JS and wrote the following example:
1 Function Class1 ()
2 {
3 VaR Oldvalue = '' ;
4 VaR Name = ' Xu ' ;
5 VaR ID = ' 1 ' ;
6 This . Setname = Function (N)
7 {
8 Oldvalue = Name;
9 Name = N;
10 This . Propertychange ( ' Name ' , Oldvalue, N );
11 }
12 This . Setid = Function (N)
13 {
14 Oldvalue = ID;
15 ID = N;
16 This . Propertychange ( ' ID ' , Oldvalue, N );
17 }
18 This . Display = Function ()
19 {
20 Alert (name + ' \ ' S ID is: ' + Id );
21 }
22 }
23 Class1.prototype = {
24 Propertychange: function (propertyname, oldvalue, newvalue)
25 {
26 Alert (propertyname + ' \ ' S value changed from ' + Oldvalue + ' To ' + Newvalue );
27 }
28 };
29
30 VaR C = New Class1 ();
31 C. setname ( ' Xu22 ' );
32 C. setid ( ' 5 ' );
33 C. Display ();
The value assignment operation on the internal variable (private variable) of the object is extracted into the method, and the corresponding variable value change callback method is triggered in this method.
In this way, you can monitor variable changes, but on the official website of Firefox, there is a stuff called object. Watch (unwatch) that can be used to monitor variable changes.
Unfortunately, this method cannot run normally in IE and other browsers. I searched the internet and found
Http://stackoverflow.com/questions/1269633/javascript-watch-for-object-properties-changes
Find the following:
1 If ( ! Object. Prototype. Watch)
2 {
3 Object. Prototype. Watch = Function (Prop, Handler)
4 {
5 VaR Oldval = This [Prop], newval = Oldval,
6 Getter = Function ()
7 {
8 Return Newval;
9 },
10 Setter = Function (VAL)
11 {
12 Oldval = Newval;
13 Return Newval = Handler. Call ( This , Prop, oldval, Val );
14 };
15 If ( Delete This [Prop])
16 {
17 If (Object. defineproperty) // Ecmascript 5
18 {
19 Object. defineproperty ( This , Prop, {Get: getter, set: setter });
20 }
21 Else If (Object. Prototype. _ definegetter __ && Object. Prototype. _ definesetter __)
22 {
23 Object. Prototype. _ definegetter _. Call ( This , Prop, Getter );
24 Object. Prototype. _ definesetter _. Call ( This , Prop, setter );
25 }
26 }
27 };
28 }
29
30 If ( ! Object. Prototype. unwatch)
31 {
32 Object. Prototype. unwatch = Function (PROP)
33 {
34 VaR Val = This [Prop];
35 Delete This [Prop];
36 This [Prop] = Val;
37 };
38 }
Listen to the assignment operation through _ definesetter ~~~
OK. Add to favorites.