// 寄生函数 function obj(o){ function Fn(){}; Fn.prototype = o; // 把指针转回给自己 return new Fn(); } function create(o,oBj){ var _o = obj(o.prototype); _o.constructor = oBj; oBj.prototype = _o; return _o; } function B(name,age){ this.name = name; this.age = age; } B.prototype.run = function(){ return this.name + this.age; } function D(name,age){ B.call(this,name,age); } create(B,D); var box = new D('gaga',236); console.log(box.constructor)
时间: 2024-10-20 01:50:45