zoukankan      html  css  js  c++  java
  • js的面向对象编程

      给阳光网写了一些js代码,想进行oo封装。毕竟对于常用的方法需要整理归纳,oo是比较理想合适。

      参考了prototype的实现,感觉很不错,修改一下为:

      

            var generateClass = function(properties, baseclass) {
                
    var c = function() {
                    
    if (this.initialize) {
                        
    this.initialize.apply(this, arguments);
                    }
                };
                
                
    /**
                 * 类原型拓展
                 * @param {Object} methods
                 * @param {Object} spuerclass
                 
    */
                c.implement 
    = function(methods, spuerclass) {
                    
    if (spuerclass) {

                        
    var parent = new spuerclass();
                        
    for (property in parent) {
                            
    this.prototype[property] = parent[property];
                        }
                        c.base 
    = spuerclass.prototype;
                    }
                    
                    
    if (methods) {
                        
    for (property in methods) {
                            
    this.prototype[property] = methods[property];
                        }
                    }
                };
                
                c.implement(properties, baseclass);
                
                
    /**
                 * 类自身静态属性拓展
                 * @param {Object} methods
                 
    */
                c.extend 
    = function(methods) {
                    
    for (property in methods) {
                        
    this[property] = methods[property];
                    }
                };
                
                
    return c;
            };


        $O 
    = generateClass;

      使用也比较简单方便:

      var c1 = $O({

      name:''

      });

      var o1 = new c1();

      在此就不累述。

      有了这个,为阳光网方便实现了很多js。感觉上容易分析了。

  • 相关阅读:
    【线程退出】linux线程退出的几个函数
    Apache Doris编译安装记录
    你所不知道的java编程思想
    thinking in java知识小记(一)
    一个程序员的修炼之路
    解决linux不能使用chmod更改权限的问题
    centos6.5配置无线网络
    ubuntu16.04 server安装小记
    vim的基本使用方法
    微信之父张小龙经典演讲164页PPT:《微信背后的产品观》
  • 原文地址:https://www.cnblogs.com/kathmi/p/1427585.html
Copyright © 2011-2022 走看看