javascript中實現類繼承的方法

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Inherit1.3.1.aspx.cs" Inherits="Inherit1" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>無標題頁</title>
  6.     
  7.     <script language="javascript">
  8.      <!--
  9.        
  10.        //定義一個靜態的方法
  11.        Object.extend = function(destination,source){
  12.        
  13.         for(property in source){
  14.         
  15.         destination[property]=source[property];
  16.         }
  17.        }
  18.        //通過Object類爲每個對象添加方法extend
  19.        Object.prototype.extendfunction(object){
  20.        
  21.        return Object.extend.apply(this,[this,object]);
  22.        }
  23.        
  24.        Function.prototype.inheritfunction(object){
  25.        
  26.          for(var p in object.prototype)
  27.          {
  28.           this.prototype[p]=object.prototype[p];
  29.          }
  30.        }
  31.          
  32.         
  33.         //定義一個對象class1
  34.         function class1()
  35.         {
  36.          
  37.         }
  38.         class1.prototype={
  39.           method:function(){alert("class1");},
  40.           method2:function(){alert("method2 of class1")}
  41.         }
  42.         //定義class2
  43.         function class2()
  44.         {
  45.           
  46.         }
  47. //        class2.prototype=(new class1()).extend({
  48. //        method:function(){
  49. //         alert("Class2");
  50. //         }
  51. //        }); 
  52.         
  53.         //定義class2的對象成員
  54.        
  55.         class2.inherit(class1);
  56.         
  57.         class2.prototype.method2=function()
  58.         {
  59.           alert("class2");
  60.         }
  61.         
  62.         var obj1 = new class1();
  63.         
  64.         var obj2new class2();
  65.         obj1.method();
  66.         obj2.method();
  67.         
  68.         
  69.      -->
  70.     </script>
  71. </head>
  72. <body>
  73.     <form id="form1" runat="server">
  74.     <div>
  75.     
  76.     </div>
  77.     </form>
  78. </body>
  79. </html>


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章