日期:2014-05-16 浏览次数:20367 次
// 以下代码目的: 使得类aa获得类bb的所有功能,即拥有 .aa .bb .bbfn() function aa() {//类 aa this.aa = 'aaa'; } function bb() {//类 bb this.bb = 'bbb'; } bb.prototype.bbfn = function() { alert('bb'); }; function forEachObj(o, fn) { for (var i in o) { fn(o[i], i); } } function apply(o1, o2) { forEachObj(o2, function (value, i) { o1[i] = value; }); } function mixin(class1, class2) { apply(class1.prototype, class2.prototype); } mixin(aa, bb); var x = new aa(); console.info(x);
function mixin(class1, class2) { apply(class1.prototype, class2.prototype); class1.prototype.constructor = function() { //把构造函数改成任意函数,但没有弹出alert alert('new'); }; }
function mixin(class1, class2) { //apply(class1.prototype, class2.prototype); class1.prototype = new class2(); class1.prototype.constructor = class1; }