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.