首页 > 代码库 > javascript里的继承

javascript里的继承

js里面继承的方式,

1. 类式继承,通过构造函数的继承

 1 function extend(subClass, superClass){ 2     var F = function(){}; 3     F.prototype = superClass.prototype; 4     subClass.prototype = new F(); 5     subClass.prototype.constructor = subClass; 6  7     subClass.superclass = superClass.prototype; // optional 8     if (superClass.prototype.constructor == Object.prototype.constructor){ 9         superClass.prototype.constructor = superClass;10     }  11 }

 

 

2. 原型式继承,通过对象的继承

function clone(object){    function F(){}    F.prototype = object;    return new F();}