prototype-1.3.1.js中的类继承实现的改进

来源:互联网 发布:网络出版的商业价值 编辑:程序博客网 时间:2024/05/21 17:18
  1.   <script language="javascript">
  2.      Object.add = function(obj){
  3.       for(var p in obj)
  4.       {
  5.         this[p]=obj[p]
  6.       }
  7.       return this;
  8.      }
  9.      
  10.      Object.prototype.add=function(obj){
  11.      return Object.add.apply(this,[obj]);
  12.      
  13.      }
  14.      
  15.      function class1()
  16.      {
  17.       
  18.        this.method=function(){
  19.         alert("class1");
  20.        }
  21.      }
  22.      function class2(){
  23.        
  24.      }
  25.      class2.prototype=(new class1()).add({
  26.       method:function(){alert("class2");}
  27.       }
  28.       )
  29.       
  30.      var obj1 = new class1();
  31.      var obj2 = new class2();
  32.      obj1.method();
  33.      obj2.method();
  34.      
  35.     </script>