How JavaScript monitors variable changes

Source: Internet
Author: User

The examples in this article describe the way JavaScript monitors variable changes. Share to everyone for your reference. The specific analysis is as follows:

You should know that in C # for attributes, files, etc. change monitoring is very simple, because there are commissioned (event), FileSystemWatcher and other good dongdong support.

So how do you monitor changes to variables in JavaScript? First, I followed the C # attribute to the JS operation, wrote the following example:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 function Class1 () {var oldvalue= '; var name= ' Xu '; var id= ' 1 '; this.setname=function (n) {oldvalue=name; name=n; this.pro Pertychange (' name ', oldvalue,n); } this.setid=function (n) {oldvalue=id; Id=n this.propertychange (' id ', oldvalue,n);} this.display=function () {alert ( Name+ ' s ID is: ' +id '; } class1.prototype={propertychange:function (propertyname,oldvalue,newvalue) {alert (propertyname+ ' s value changed From ' +oldvalue+ ' to ' +newvalue); } }; var c=new Class1 (); C.setname (' xu22 '); C.setid (' 5 '); C.display ();

An assignment operation on an object's internal variable (private variable) is extracted into the method, and the corresponding variable value change callback method is triggered in the method.

It is supposed to be able to monitor variable changes, but on Firefox's official website there is a unwatch called Object.watch, which can be used to monitor variable changes.

Unfortunately, this method does not work properly in browsers such as IE. I searched the internet for a while

Http://stackoverflow.com/questions/1269633/javascript-watch-for-object-properties-changes

Find the following dongdong:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

Through __definesetter__ to the assignment operation monitoring ~ ~ ~

OK, good stuff. Collect it.

I hope this article will help you with your JavaScript programming.

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.