ITeye博客

js链式编程

javapolo · 2013-12-26 11:26 · js · Web前端 · Markdown

js链式编程 实现对象方法动态扩展 (function(window){

window._MethodManager=function(obj)
{
    //要处理的数据对象
    this.data=obj;
    //要添加的方法
    this.methodNames=;
}
//添加方法
_MethodManager.prototype.addMethod=function(name,fn)
{
   //向原型链添加方法
    _MethodManager.prototype=fn;
    //添加方法名称
    this.methodNames.push(name);
    return this;
}

})(window);

var fm=new _MethodManager("this is a data"); //添加方法一 fm.addMethod('methodone',function(){ var handdata=this.data; //hand the data this.data=this.data+" hand one "; return this; }); //添加方法2 fm.addMethod('methodtwo',function(){ var handdata=this.data; //hand the data this.data=this.data+" hand two "; return this; });

..添加其他的方法

console.dir(fm); console.log(fm.methodone().data); console.log(fm.methodone().methodtwo().data);

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。