您的位置:首页 > Web前端 > JavaScript

javascript继承的几种方式

2016-08-05 12:20 274 查看
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; 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  js