Javascript里的类构造;以前不是很常用到,Javascript1.5有更多特性,可惜IE不支持。
自定义类:
- //constructor;
- function Alerter(aMsg,aTimes)
- {
- //properties;
- this.mMsg=aMsg;
- this.mTimes=aTimes;
- //methods;
- this.start=AlerterStart;
- }
- function AlerterStart()
- {
- var i;
- for(i=0;i<this .mTimes;i++)
- {
- alert(this.mMsg+" time:"+(i+1));
- }
- return;
- }
- //instances;
- var objAlerter=new Alerter("funny",5);
- document.write("<a href=\"javascript:objAlerter.start();\">TEST GO!");
添加或重载固有类的方法/属性:
- //Array;
- function Array_sum()
- {
- var t=0;
- for(var i=0;i<this .length;i++)
- {
- t+=this[i];
- }
- return t;
- }
- //add a method;
- Array.prototype.sum=Array_sum;
- var a=new Array(1,2,3,4,5);
- document.write("<a href=\"javascript:alert(a.sum());\">TEST GO!");
- http://www.yaszone.com/wp/index.php/14