无梦家园

无梦家园
posts - 21, comments - 151, trackbacks - 8, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理
      在javascript中,函数其实就是"类",创建一个类就是写一个函数,创建一个类的实例也相当简单,如下示例:
//class
var MyClass = function(firstname,lastname){    
    
this._firstname = firstname;
    
this._lastname = lastname;
    
this._name = this._firstname + ":" + this._lastname;
}

MyClass.prototype 
= {
    _firstname: 
null,
    _lastname: 
null,
    _name: 
null,
    
    get_name: 
function(){
        
return this._name;
    }
,
    set_firstname: 
function(value){
        
this._firstname = value;
    }
,
    set_lastname: 
function(value){
        
this._lastname = value;
    }

}

//instance
var m = new MyClass("chun","rong");
alert(m.get_name());
      
      在本例中,var m = new MyClass("chun","rong");时,传递进参数,在MyClass函数体内,直接进行了"对象状态初始化"操作,如"this._name = this._firstname + ":" + this._lastname;",请着重注意"对象状态初始化"这个词语.
      给类引入"initialize"初始化操作这个概念,有什么好处和必要呢?下面演示引入"initialize"概念后,上面的示例应该怎么写:
//class
var MyClass = function(firstname,lastname){    
    
this._firstname = firstname;
    
this._lastname = lastname;    
}

MyClass.prototype 
= {
    _firstname: 
null,
    _lastname: 
null,
    _name: 
null,
    
    get_name: 
function(){
        
return this._name;
    }
,
    set_firstname: 
function(value){
        
this._firstname = value;
    }
,
    set_lastname: 
function(value){
        
this._lastname = value;
    }
,
    initialize: 
function(){
        
this._name = this._firstname + ":" + this._lastname;//对象初始化操作.
    }
    
}

//instance
var m = new MyClass("chun","rong");
alert(m.get_name());
//当没有进行initialize操作时,对象的状态不能得到很好的初始化
m.initialize();//初始化
alert(m.get_name());

      要理解这段js,就必须要区分:
    this._firstname = firstname;
    
this._lastname = lastname;   
      和
this._name = this._firstname + ":" + this._lastname;//对象初始化操作.
      这两段代码的"逻辑意义".
      第一段代码是"赋值"逻辑代码,第二段代码,才是真正影响MyClass对象实例逻辑行为的代码.
      "赋值"逻辑代码是可有可无的,这要看对象的某个属性对对象状态/行为的影响程度!
      下面请看这段js:
function setProperties(component, properties){
    
for (var name in properties) {
        
var val = properties[name];
        
var setter = component["set_" + name];
        
if (typeof(setter) === 'function') {
            setter.apply(component, [val]);            
        }
       
    }
    
}


//class
var MyClass = function(){
}

MyClass.prototype 
= {
    _firstname: 
null,
    _lastname: 
null,
    _name: 
null,
    
    get_name: 
function(){
        
return this._name;
    }
,
    set_firstname: 
function(value){
        
this._firstname = value;
    }
,
    set_lastname: 
function(value){
        
this._lastname = value;
    }
,
    initialize: 
function(){
        
this._name = this._firstname + ":" + this._lastname;//对象初始化操作.
    }
    
}

//instance
var m = new MyClass();
setProperties(m,
{firstname:"fan",lastname:"rong"});
m.initialize();
//初始化
alert(m.get_name());
      这里的MyClass类的构造器,干脆去除掉了参数,而"对象状态初始化"所必须的基本属性,则必须得到提供.这里通过setProperties(m,{firstname:"fan",lastname:"rong"})方法得到实现.当然,如果确实相当重要不可或缺,可以在类的构造器内直接指定!比如:MS AJAX的control,有一个element属性就是不可或缺的.
      
      这样的写法,有什么好处?对,关键就在于"好处"!
      好处其实已经够明显的了.对比前后的示例,你就会发现最后的示例,MyClass类最清晰简单,代码量最少!

      MS AJAX就是采用了这样的组件类设计方式.它提供了一个$create方法,来支持从Sys.Component基类派生的类的所有对象实例创建.下面实现我们自己的一个简单的$create:
function setProperties(component, properties){
    
for (var name in properties) {
        
var val = properties[name];
        
var setter = component["set_" + name];
        
if (typeof(setter) === 'function') {
            setter.apply(component, [val]);            
        }
       
    }
    
}

var $create = function(type,properties){
    
var component = new type();
    setProperties(component,properties);
    component.initialize();
//初始化 
    return component;
}

//class
var MyClass = function(){
}

MyClass.prototype 
= {
    _firstname: 
null,
    _lastname: 
null,
    _name: 
null,
    
    get_name: 
function(){
        
return this._name;
    }
,
    set_firstname: 
function(value){
        
this._firstname = value;
    }
,
    set_lastname: 
function(value){
        
this._lastname = value;
    }
,
    initialize: 
function(){
        
this._name = this._firstname + ":" + this._lastname;//对象初始化操作.
    }
    
}

//instance
var m = $create(MyClass,{firstname:"fan",lastname:"rong"});
alert(m.get_name());


Feedback

#1楼    回复  引用    

2007-06-20 13:18 by sigmazel [未注册用户]
相当的强悍。呵呵。

MSAJAX有一些是根据JS的特点演化出来的模式,适可而止。

#2楼    回复  引用    

2007-06-21 08:35 by Heresy.Mc [未注册用户]
赞一个!!!
不错的说!!!

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-06-20 12:37 编辑过
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》



相关文章:

相关链接: