[javascript]代码库
//寄生函数
function create(box, desk) {//继承私有的属性和prototype中的属性
var f = obj(box.prototype);
f.constructor = desk; //调整原型构造指针
desk.prototype = f;
}
function Box(name, age) {
this.name = name;
this.age = age;
}
Box.prototype.run = function () {
return this.name + this.age + '运行中...'
}
function Desk(name, age) {
Box.call(this, name, age); //对象冒充
}
//通过寄生组合继承来实现继承
create(Box, Desk); //这句话用来替代Desk.prototype = new Box();
var desk = new Desk('Lee', 100);
alert(desk.run());
alert(desk.constructor);
[代码运行效果截图]