<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>

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-26
  • 2021-09-30
猜你喜欢
  • 2021-11-03
  • 2021-07-20
  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
  • 2021-03-13
  • 2021-04-12
相关资源
相似解决方案