asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0
CSS=function(key,val){
 var $=this.style;
 if(val!==undefined){
  $[key]=val;
  return this;
 }
 if ($[key])return $[key]; 
    if (this.currentStyle) return this.currentStyle[key]
    if (document.defaultView && document.defaultView.getComputedStyle){
            key = key.replace(/([A-Z])/g,"-$1").toLowerCase();
            var V = document.defaultView.getComputedStyle(this,"");
            return V && s.getPropertyValue(key);
    }
    return null;
}
<script>
// 目标函数
function base(){
 alert('原函数')
}
// 装饰器
Decorator={
 beginList:[]//前置列表
 ,endList:[]//后置列表
 ,append:function (fn){
  this.endList.push(fn);
 }
 ,before:function (fn){
  this.beginList.splice(0,0,fn)
 }
 ,wrap:function (fn/* 待装饰的函数 */){
  var $=this,$1=[],$2=[];
  // 拷贝两个列表内容放到闭包
  for (var i=0;i<$.beginList.length;i++ )
   $1[i]=$.beginList[i];
  for (var i=0;i<$.endList.length;i++ )
   $2[i]=$.endList[i];
  try{
   // 返回装饰完毕的函数
   return function (){
    for (var i=0;i<$1.length;i++ )
     $1[i]();
    if(typeof fn == 'function')fn();
    for (var i=0;i<$2.length;i++ )
     $2[i]();
   }   
  }finally{
   // 用finally技巧在返回后清空缓存的列表,以备下次使用
   this.beginList.length=this.endList.length=0;
  }
 }
}
Decorator.append(function (){
 alert('第1个追加函数')
});
Decorator.append(function (){
 alert('第2个追加函数')
});
Decorator.before(function (){
 alert('第1个前插函数')
});
Decorator.before(function (){
 alert('第2个前插函数')
});
v=Decorator.wrap(base);
alert('装饰器的列表长度现在为: '+Decorator.endList.length)
v()
</script>
posted on 2009-02-26 17:38 汪杰 阅读(212) 评论(0)  编辑 收藏 引用 所属分类: javascript
只有注册用户登录后才能发表评论。

<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 456691
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜