js实现继承
一、使用构造函数实现"继承
参考:Javascript面向对象编程(二):构造函数的继承
 function extend(Child, Parent) {
    var F = function(){};  // 利用空对象作为中介   F是空对象,所以几乎不占内存。
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child; //不会影响Parent
Child.uber = Parent.prototype; // 备用
}
二、非构造函数的继承
参考:Javascript面向对象编程(三):非构造函数的继承
// 浅拷贝
function extendCopy(p) {
var c = {};
    for (var i in p) { 
      c[i] = p[i];
    }
c.uber = p;
    return c;
  }
// 深拷贝
function deepCopy(p, c) {
var c = c || {};
for (var i in p) {
if (typeof p[i] === 'object') {
c[i] = (p[i].constructor === Array) ? [] : {};
deepCopy(p[i], c[i]);
} else {
c[i] = p[i];
      }
    }
    return c;
  }
 
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号