//掺杂类实现聚合(有的时候,我们需要某个或多个类里的一些方法函数) //将要被聚合的函数 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(','))