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();
}

 

 

javascript里的继承,布布扣,bubuko.com

javascript里的继承

上一篇:Eclipse导入Java项目时“No projects are found to import”错误的处理


下一篇:菜鸟学习javascript实例教程