Javascript variable monitoring

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.