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;
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;
相关文章推荐
- JavaScript中对象的定义和继承的几种方式
- JavaScript中实现继承的几种方式
- Javascript中的几种继承方式对比分析
- JavaScript中实现继承的几种方式的使用和分析
- javascript 实现继承的几种方式
- javascript几种实现继承方式
- Javascript编程中几种继承方式比较分析
- 详述JavaScript实现继承的几种方式(推荐)
- Javascript中的几种继承方式比较
- JavaScript的几种继承方式
- JavaScript继承的几种方式解析
- Javascript编程中几种继承方式比较分析
- javascript的几种继承方式
- 详述JavaScript实现继承的几种方式(推荐)
- JavaScript中对象的定义和继承的几种方式
- javascript 继承的几种方式
- Javascript中的几种继承方式
- javascript的几种继承方式
- javascript的几种继承方式
- Javascript中的几种继承方式对比分析