JS继承封装
<script>
var extend = function (subClass, superClass) {
//1.继承类的中间类
var Tmp = function() {};
//2.将父类的原型传递给中间类
Tmp.prototype = superClass.prototype;
//3.设值继承关系
subClass.prototype = new Tmp();
subClass.prototype.constructor = subClass;
//4.将父类的this转换成当前类的this
subClass.convertASThis = superClass.prototype;
}
function Person(name) {
this.name = name;
}
function Author(name, books) {
//使用call方法将父类的this转换为本类的this,并将name属性合并到当前类
Author.convertASThis.constructor.call(this, name);
this.books = books;
this.getInfo = function() {
return this.name + " -> " + this.books;
}
}
onload = function() {
extend(Author, Person);
var a = new Author("aa", "bb");
alert(a.getInfo());
}
</script>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。