javascript是怎么继承的介绍
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript是怎么继承的介绍,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3433字,纯文字阅读大概需要5分钟。
内容图文
![javascript是怎么继承的介绍](/upload/InfoBanner/zyjiaocheng/369/d27b580dd4be4f1c8f0f9add6c3a6d17.jpg)
代码如下:
function A(){
this.funB = function(){
alert('A:funB');
};
}
A.prototype = {
funA:function(){
alert('A:funA');
}
};
function B(){
}
function extend(sub,parent){
sub.prototype = new parent();
sub.prototype.constructor = sub;
}
extend(B,A);
var b = new B();
b.funA(); // out 'A:funA'
b.funB(); // out 'A:funB'
alert(b instanceof A); // out "true"
想必大家一眼就看出什么意思了,先是定义了A,B两个类,然后使用extend方法来让B继承A类。extend的原理就是让父类 new 到子类的prototype上。
用instanceof来检测也为true,想要让instanceof为true,那就必须两个类的prototype对象要为同一个object,不管是间接或直接的。
这样的方式有没有问题呢?在通常面向对象语言中,子类在继承父类时,是不会触发父类的构造函数执行,而这里是父类是在继承时执行的。
第二个阶段
代码如下:
function A(){
this.Astr = 'hello A';
}
A.prototype = {
funA:function(){
alert(this.Astr);
}
};
function B(){
arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
this.Bstr = 'hello B';
}
B.prototype = {
funB:function(){
alert(this.Bstr);
}
};
function C(){
arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
alert(this.Astr);
alert(this.Bstr);
}
function extend(sub,parent){
var subproto = sub.prototype;
sub.prototype = parent.prototype;
typeof subproto != 'object' && (subproto = {});
typeof sub.prototype != 'object' && (sub.prototype = {});
for(var i in subproto){
sub.prototype[i] = subproto[i];
}
sub.superclass = parent;
}
//B 继承 A
extend(B,A);
//C 继承 B
extend(C,B);
var c = new C(); // out 'hello A','hello B'
c.funA(); //out 'hello A'
c.funB(); // out 'hello B'
alert(c instanceof A) // out true
alert(c instanceof B) // out true;
这里对extend方法做了一些改动,这里有个约定,每个子类都拥有一个superclass的属性,用来引用她所继承的父类,用一个空函数proto来获得父类的prototype,实例化给子类的prototype,这样就没有执行父类构造器。
而是在子类的构造器中用下来一段代码来执行约定要的父类构造器。
代码如下:
arguments.callee.superclass && arguments.callee.superclass.apply(this,argumengs);
这样就完成了类的继承。
对于上面的代码有没有更方便的继承写法呢,修改Function的原型来看看:
代码如下:
Function.prototype.extend = function(parent){
var subproto = this.prototype;
this.prototype = parent.prototype;
typeof subproto != 'object' && (subproto = {});
typeof this.prototype != 'object' && (this.prototype = {});
for(var i in subproto){
this.prototype[i] = subproto[i];
}
this.superclass = parent;
return this;
}
function A(){
this.Astr = 'hello A';
}
A.prototype = {
funA:function(){
alert(this.Astr);
}
};
var B = function(){
arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
this.Bstr = 'hello B';
}
B.prototype = {
funB:function(){
alert(this.Astr);
}
};
B.extend(A);
var C = function(){
arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
alert(this.Astr);
alert(this.Bstr);
}.extend(B);
var c = new C(); // out 'hello A','hello B'
c.funA(); //out 'hello A'
c.funB(); // out 'hello B'
alert(c instanceof A) // out true
alert(c instanceof B) // out true;
这里的extend做的事情是: subproto引用子类的原prototype ,将子类的prototype 指向 父类的prototype对象,这样就继承了父类(这样的目的是让 子类实例 instanceof 父类 为 true)。然后历遍subproto,将原prototype的成员添加到现prototype上,这样子类重名的重名的成员就会覆盖父类的成员。最后将子类的属性superclass 指向 父类。
js继承的关键就是保持原型链的唯一性,instanceof就以判断实例的__proto__是否和父类的prototype为同一Object.
作者 cnblogs OD
内容总结
以上是互联网集市为您收集整理的javascript是怎么继承的介绍全部内容,希望文章能够帮你解决javascript是怎么继承的介绍所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。