hi, 欢迎访问我的博客

js函数聚合

//掺杂类实现聚合(有的时候,我们需要某个或多个类里的一些方法函数)


//将要被聚合的函数
var JSON = {
    toJsonString: function () {
        var output = [];
        for (key in this) {
            //这里this指代调用者
            output.push(key + '-->' + this[key]);
        }
        return output;
    }
};


function mixin(receivingClass, givingClass) {
    for (methodName in givingClass) {
        if (!receivingClass.__proto__[methodName]) {
            receivingClass.__proto__[methodName] = givingClass[methodName];
        }
    }
}
var o = {
    name: 'long',
    age: 22
}
mixin(o,JSON);
document.write(o.toJsonString().join(','))

posted @ 2017-10-17 07:40  打静爵  阅读(1031)  评论(0编辑  收藏  举报