下面的代码运行良好
- Perl code
function A(text,value) { var num=0; this.GetNum=function(){return num;}; this.AddNum=function(){++num;} this.Text=text; this.Value=value; } var obj=new A("Hi","World"); var num; obj.AddNum(); num=obj.GetNum();//此时num应为1; obj.AddNum(); num=obj.GetNum();//此时num应为2;
怎样写JavaScript,达到下面这样的效果:
- Perl code
var obj=new A("Hi","World"); obj.Text="Hello";//这一句执行完后num自动加一(用 = 赋值时能执行其它语句) num=obj.GetNum();//此时num应为1;
------解决方案--------------------
可以参考
http://articles.sitepoint.com/print/javascript-objects
的代码
onpropertychange
------解决方案--------------------
用属性不好监控,改成调用方法吧
- JScript code
function A(text,value) { var num=0; this.GetNum=function(){return num;}; this.AddNum=function(){++num;} this.SetText = function(txt){ this.Text = txt; this.AddNum();}; this.Text=text; this.Value=value; } var obj=new A("Hi","World"); obj.SetText("Hello");//这一句执行完后num自动加一(用 = 赋值时能执行其它语句) num=obj.GetNum();//此时num应为1;