javascript继承的几种方式

来源:互联网 发布:安卓画图软件 编辑:程序博客网 时间:2024/06/08 06:00

1. 默认继承

function inherit(C, P) {     C.prototype = new P();}
缺点:不支持将参数传递到子构造函数中

2. 借用构造函数

function Child(a, b, c, d) {    Parent.apply(this, arguments);}
缺点:无法从原型继承任何东西


3. 借用和设置原型

function Child(a, b, c, d) {    Parent.apply(this, arguments);}Child.prototype = new Parent();
缺点:父构造函数被调用两次

4. 共享原型

function inherit(C, P) {    C.prototype = P.prototype;}
缺点:子对象或孙子对象修改了原型,它将影响到所有的父对象和祖先对象


5.临时构造函数

function(C, P) {    var F = function() {};    F.prototype = P.prototype;    C.prototype = new F;}

存储超类:C.uber = P.prototype;

重置构造函数:C.prototype.constructor = C; 

0 0
原创粉丝点击