Javascript下的伪OOP用法测试

JavaScript代码
  1. "text/javascript">  
  2.   
  3. // 方法一  
  4. var cellphone = {  
  5.     number: 13684042440,  
  6.     dial: function  () {  
  7.           
  8.     }  
  9. };  
  10.   
  11. // 方法二  
  12. var cellphone = new Object();  
  13. cellphone.number = 13484042440;  
  14. cellphone.dial = function () {  
  15.     alert("Calling "+this.number);  
  16. }  
  17.   
  18. cellphone.dial();  
  19.   
  20. // 给已有的类追加方法  
  21. String.prototype.trim = function () {  
  22.     var tmp = this.replace(/^\s+/,);  
  23.     tmp = tmp.replace(/\s+$/,);  
  24.     return tmp;  
  25. }  
  26.   
  27. var str = "  sfdfd  sfsd   ";  
  28. str2=str.trim();  
  29. alert("’"+str+"’");  
  30. alert("’"+str2+"’");  
  31.   
  32. function Beesn() {  
  33.     this.member = new Array(‘Kevin’,‘Vonsk’,‘Edison’,‘Michael’,‘Whl’),  
  34.     this.list = function () {  
  35.         for(var i=0;i<this.member.length;i++) {  
  36.             alert(this.member[i]);  
  37.         }  
  38.     },  
  39.     this.show = function () {  
  40.         this.member = this.member.shift();  
  41.         alert(this.member);  
  42.         //for(i in this.member) {  
  43.         //  alert(this.member[i]);  
  44.         //}  
  45.     }  
  46. }  
  47. var our = new Beesn;  
  48. our.show();  
  49.   
  50.   
  51. //arguments 对象的用法。  
  52. function ArgTest(a, b, c){  
  53.    var i, s = "The ArgTest function expected ";  
  54.    var numargs = arguments.length;     // 获取被传递参数的数值。  
  55.    var expargs = ArgTest.length;       // 获取期望参数的数值。  
  56.    if (expargs < 2)  
  57.        s += expargs + " argument. ";  
  58.    else  
  59.        s += expargs + " arguments. ";  
  60.    if (numargs < 2)  
  61.        s += numargs + " was passed.";  
  62.    else  
  63.        s += numargs + " were passed.";  
  64.     s += "\n\n"  
  65.    for (i =0 ; i < numargs; i++){      // 获取参数内容。  
  66.     s += "   Arg " + i + " = " + arguments[i] + "\n";  
  67.     }  
  68.    return(s);                          // 返回参数列表。  
  69. }  
  70. alert( ArgTest(1,our,our) );  
  71.   
  72. // caller demo  
  73. function callerDemo() {  
  74.     if (callerDemo.caller) {  
  75.          var a= callerDemo.caller.toString();  
  76.          alert(a);  
  77.      } else {  
  78.          alert("this is a top function");  
  79.      }  
  80. }  
  81. function handleCaller() {  
  82.      callerDemo();  
  83. }  
  84. callerDemo();  
  85.   
  86. //callee可以打印其本身  
  87. function calleeDemo() {  
  88.      alert(arguments.callee);  
  89. }  
  90. //用于验证参数  
  91. function calleeLengthDemo(arg1, arg2) {  
  92.     if (arguments.length==arguments.callee.length) {  
  93.          window.alert("验证形参和实参长度正确!");  
  94.         return;  
  95.      } else {  
  96.          alert("实参长度:" +arguments.length);  
  97.          alert("形参长度: " +arguments.callee.length);  
  98.          aert(calleeLengthDemo.length);  
  99.      }  
  100. }  
  101. //递归计算  
  102. var sum = function(n){  
  103.   if (n <= 0)                          
  104.   return 1;  
  105.   else  
  106.     return n + arguments.callee(n – 1);  
  107. }  
  108. calleeLengthDemo(1,2,3);  
  109.   
  110. var vehicle=Class.create();  
  111. vehicle.prototype.initialize= function(type)  
  112. {  
  113.         this.type=type;  
  114. }  
  115. vehicle.prototype.showSelf= function()  
  116. {  
  117.       alert("this vehicle is "this.type);  
  118. }  
  119. var Class = {  
  120.    create: function() {  
  121.     returnfunction() {  
  122.       this.initialize.apply(this, arguments);  
  123.      }  
  124.    }  
  125. }  
  126. var moto=new vehicle("Moto");  
  127. moto.showSelf();  
  128.   
  129.   
  130. // 测试extend  
  131. Object.extend = function(destination, source) {  
  132.   for (property in source) {  
  133.     destination[property] = source[property];  
  134.   }  
  135.   return destination;  
  136. }  
  137. Object.prototype.extend = function(object) {  
  138.   return Object.extend.apply(this, [this, object]);  
  139. }  
  140.   
  141. var obj = new Object();  
  142. obj.extend({  
  143.     f1: function () {  
  144.         alert(‘f1′);  
  145.     },  
  146.     f2: function () {  
  147.         alert(‘f2′);  
  148.     }  
  149. });  
  150.   
  151. obj.f2();  
  152.   
  153.   
  154. String.prototype.isEmail = function () {  
  155.     return /^[0-9a-z\-_\.]+@[0-9a-z\-_\.]+\.[a-z]+$/i.test(this);  
  156. };  
  157.   
  158.   
  159. var str = "combbs@msn.com";  
  160. alert(  str.isEmail()  );  
  161.   
  162. //alert(/^[0-9a-z\-_\.]+@[0-9a-z\-_\.]+\.[a-z]+$/i.test(str));  
  163. //–>  
  164.   

测试方法:

请一段一段的测试。测试一段时,先把别的行注释。