为 javascript 面向对象编程封装

 

 javascript 是一个基于对象的脚本语言,不是一个真正的面向对象的脚本语音,因此javascript的面向对象纯属于鸡肋。

 

不过还好,javascript是一个非常灵活的语言。有很多间接的方式实现类的定义、类的继承。

 

如《javascript高级程序设计》P78P91所描述的有很多方式,作者Nicholas C. Zakas建议使用混合的构造函数/原型方式(P84)定义类,并建议使用混合的对象冒充/原型链方式(P96)继承类。

 

因此我对它进行了封装:

 

  1  /* *************************************************************
  2 
  3      Class.js version 1.2.0
  4      $Class Namespace
  5      author Tangoboy
  6      This anonymous function acts as a namespace wrapper for the rest
  7      of the methods. Methods are then assigned to the window object
  8      using: window['$Class']['methodName'] = methodReference;
  9      Copyright 2010
 10      Dual licensed under the MIT or GPL Version 2 licenses.
 11 
 12  http://blog.tangoboy.com/
 13 
 14      2010-08-16 0:28
 15 
 16  **************************************************************
 17      Example:
 18    //******创建类******
 19      var man  = $Class.create({
 20          //构造方法
 21          __:function(m){
 22              this.name = m||"uname";
 23 
 24              //私有属性
 25              var money = 100;
 26              //私有方法
 27              function buy(){
 28 
 29              }
 30              this.getmoney = function(){
 31                  return money;
 32              }
 33          },
 34          //共有属性
 35          sex:"男",
 36          //共有方法
 37          showname:function(){
 38            alert(this.name);
 39          }
 40      });
 41 
 42      var m = new man("abc");
 43 
 44    //******继承******
 45 
 46      var chunGe = $Class.inherit(man);
 47 
 48      //or
 49 
 50      var chunGe = $Class.inherit(man,{
 51          __:function(m){
 52              this.type = m||"chunGe";
 53 
 54              //私有属性
 55              var money = 500,baqi = 10;
 56              this.getmoney = function(){
 57                  alert(money);
 58              };
 59              this.baqi = function(){
 60                  alert(baqi);
 61              }
 62          },
 63          alse:function(){
 64            alert(this.sex);
 65          }
 66      });
 67 
 68      var c = new chunGe("chunchun");
 69 
 70          //检测c是否为chunGe的实例
 71          //alert(c instanceof chunGe);
 72          //检测c是否为man的实例
 73          //alert(c instanceof man);
 74          //构造函数
 75          //alert(c.constructor);
 76 
 77    //******包含,添加原型成员******
 78    var run = function(){
 79      alert("run");
 80    };
 81    var jump = function(){
 82      alert("jump");
 83    };
 84    var baojuhua = function(){
 85      alert("baojuhua");
 86    };
 87    $Class.include(chunGe,{"run":run,"jump":jump,"baojuhua":baojuhua});
 88 
 89  */
 90 
 91  ;;( function (){
 92  var  opt  =  Object.prototype.toString,
 93  isFun  =   function (f){ return  opt.call(f) === " [object Function] " },
 94  isObj  =   function (o){ return  opt.call(o) === " [object Object] " };
 95  window[ ' $Class ' =  {
 96       // 创建一个类  混合构造函数/原型方式
 97      create:  function (config) {
 98           var  obj  =   function (){},config  =  config || {};
 99           // 过滤构造方法和原型方法
100          obj  =  obj.prototype.constructor  =  config[ " __ " ] || obj;
101           delete  config[ " __ " ];
102          obj.prototype  =  config;
103           return  obj;
104      },
105       // 继承  混合对象冒充/原型链方式
106      inherit: function (source,extd) {
107           if ( ! isFun(source)) return ;
108           var  obj  =  source,extd  =  extd || {},pty  =  {};
109           // 过滤构造方法和原型方法
110          obj  =  extd[ " __ " ] || obj;
111           delete  extd[ " __ " ];
112          pty  =  extd;
113           // 对象冒充
114           var  exobj  =   function (){
115              source.apply( this ,arguments);
116              obj.apply( this ,arguments);
117          };
118           // 原型链
119          exobj.prototype  =   new  source();
120           // 原型扩展
121           //exobj.prototype  =  source.prototype;
122           this .include(exobj,pty);
123          exobj.prototype.constructor  =  obj;
124           return  exobj;
125      },
126       // 原型扩展
127      include: function (target,ptys){
128           if ( ! isFun(target)){target  =   function (){};}
129           if (isObj(ptys)){
130               for (k  in  ptys){
131                  target.prototype[k]  =  ptys[k];
132              }
133          }
134           return  target;
135      }
136  };
137  })();
138 

 

 

 download: Class.js

转载于:https://www.cnblogs.com/tangoboy/archive/2010/08/03/1790412.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值