日期:2014-05-16  浏览次数:20400 次

总结下js的继承实现方式(转)

第一种 prototype 引用型原型继承
语言支持:js原生支持的继承方式 构造器的的prototype属性作为类的原型 每个该类的对象都持有一个到原型的引用 当对象中的属性不存在时 可以访问原型的属性
代码示例:

<script>
function parent(){
    this.x=10;
}
function child(){
}
child.prototype=new parent();
var childObj=new child();
alert(childObj.x);
</script>

?

?

第二种 复制型原型继承
语言支持:js new运算符的性质 当构造函数return值为非空对象时 new表达式返回return的对象
代码示例:

<script>
function parent(){
    this.x=10;
}
function child(){
    var ret=new parent();
    ret.y=20;
    return ret;
}

var childObj=new child();
alert(childObj.x);</script>

?

?

第三种 类继承 属性抄写
语言支持:for in枚举对象所有属性
代码:

<script>
function parent(){
    this.x=10;
}
function child(){
    var parentObj=new parent();
    for(var p in parentObj)this[p]=parentObj[p];
}
var childObj=new child();
alert(childObj.x);</script>

?

?

第四种 类继承 对象冒充
语言支持: 1.动态添加和删除方法 2.函数的call和apply
代码:
用语言支持1实现的类继承

<script>
function parent(){
    this.x=10;
}
function child(){
    this.parent=parent;
    this.parent();
    delete this.parent;
}
var childObj=new child();
alert(childObj.x);</script>

?

用语言支持2实现的类继承

<script>
function parent(){
    this.x=10;
}
function child(){
    parent.call(this);
}
var childObj=new child();
alert(childObj.x);</script>

?

第五种 原型抄写
语言支持:通过修改类的原型对象 可以为一类对象添加属性和方法
代码:

<script>
function parent(){}
parent.prototype.me=function(){alert("parent")};
function child(){}
for(var p in parent.prototype)child.prototype[p]=parent.prototype[p];
var childObj=new child();
childObj.me();</script>

?

第六种 元类
语言支持: js函数都是对象 且js函数可被构造
代码:

<script>function parent(string){
    var child=new Function("this.x=10;"+string);
    return child;
}
var child=new parent("this.y=20;");

var childObj=new child();
alert(childObj.y);
</script>

?

关于混用:
如此之多的继承方式,互相组合使用的方式非常多,我把我考虑过的方式和楼下讨论的方式都总结在这里

有些方法显然不能同时用的 第一种+第二种 第六种+其他

来自asfman的 第五种和第四种的组合
代码

function a(){}
function b(){
  a.call(this);
}
for(var p in a.prototype)
{
  b.prototype[p]=a.prototype[p];
} 


我的观点: 对js对象来说 它的属性来自两部分:prototype属性和自身属性 call/apply只能继承自身属性 原型抄写只能继承prototype属性
将它们混用互相没有任何干扰 而且实现了所有属性的继承。但是我觉得,如果按规范编码的话,似乎只用其一就足够了,对一个类来说,同时给prototype和构造函数添加属性的情况很少见吧?

?

我的最爱 第二种+第三种??第二种+第四种
to be continued......