Javascript中的类的创建

  1. /**  
  2.  * 工厂方法:  
  3.  * 存在问题:重复创建对象eat  
  4.  */  
  5. function CreatePeople(name){   
  6.   var  people=new Object();   
  7.   people.name=name;   
  8.   people.eat=function(){   
  9.     alert(this.name+' is eating  !!')   
  10.   };   
  11.   return people;   
  12. }   
  13. /**  
  14.  * 构造函数法:  
  15.  * 存在问题:重复创建对象eat  
  16.  * 使用new 来创建对象,如:var p=new People('saber')  
  17.  * 在执行第一句代码前,先创建一个对象(并返回,所以构造函数最后无须return),只有用this才能访问该对象,  
  18.  *   
  19.  */  
  20. function People(name){   
  21.   this.name=name;   
  22.   this.eat=function(){   
  23.     alert(this.name+' is eating  !!')   
  24.   };   
  25. }   
  26. /**  
  27.  * 混合的构造函数/原型方式(推荐)  
  28.  */  
  29. var People1=function (name){   
  30.     this.name=name;    
  31. }   
  32. People1.prototype={   
  33.     eat:function(){   
  34.         alert(this.name+' is eating !')   
  35.     }   
  36. }   
  37. /* 自定义构造函数为:initialize  
  38.  * 在使用new创建对象的时候,执行initialize函数,  
  39.  */  
  40. var People11=function (){   
  41.     this.initialize.apply(this, arguments);   
  42. }   
  43. People11.prototype={   
  44.     initialize:function(name){   
  45.         this.name=name;   
  46.         alert(' Initializing Ok !!');   
  47.     },   
  48.     eat:function(){   
  49.         alert(this.name+' is eating !')   
  50.     }   
  51. }   
  52. /* 有以上基础,如果我们要以一种统一的方式来定义类,该如何呢?  
  53.  * 像java中定义类都采用Class关键字一样,而在JS中,没有类这个概念,  
  54.  * 那么我们首先声明一个对象MyClass  
  55.  */  
  56.   
  57. var MyClass=function(){   
  58.     return function(){//闭包   
  59.         this.initialize.apply(this, arguments);   
  60.     }   
  61. }   
  62. /*  
  63.  * 定义类:  
  64.  */  
  65. var ClassOne=MyClass();   
  66. ClassOne.prototype={   
  67.     initialize:function(name){   
  68.         this.name=name;    
  69.         alert(this.name+' initializing Ok !')   
  70.     },   
  71.     methodOne:function(){   
  72.         alert("My name is:" + this.name);   
  73.     }   
  74. }   
  75.   
  76. var ClassTwo=MyClass();   
  77. ClassTwo.prototype={   
  78.     initialize:function(name){   
  79.         this.name=name;        
  80.     },   
  81.     methodTwo:function(){   
  82.         alert("My name is:" + this.name);   
  83.     }   
  84. }   
  85.   
  86. /**  
  87.  * 采用prototype.js,它的方法和我们上边的一样:  
  88.  * var Class = {  
  89.  * create: function() {  
  90.  *  return function() {  
  91.  *    this.initialize.apply(this, arguments);  
  92.  *  }  
  93.  * }  
  94.  *}  
  95.  */  
  96. var People2=Class.create();   
  97. People2.prototype={   
  98.     //自定义的构造函数:initialize   
  99.     initialize:function(name,sex){   
  100.         this.name=name;   
  101.         this.sex=sex;   
  102.     },   
  103.     eat:function(){   
  104.         alert(this.name+' is eating ');   
  105.     },   
  106.     showSex:function (){   
  107.         alert(this.name+'\'s sex is :'+this.sex);   
  108.     }   
  109. }  
posted @ 2010-02-06 00:05  waynecn  阅读(175)  评论(0编辑  收藏  举报