---
title: "js链式编程"
author: "javapolo"
date: "2013-12-26T11:26:04Z"
tags: []
categories: ["Web前端"]
slug: "js"
draft: false
lastmod: "2013-12-26T11:26:04Z"
id: 1995658
url: "https://javapolo.iteye.com/blog/1995658"
---

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);