functioin extend(subClass,superClass){
var F = function(){};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superClass = superClass.prototype;
if(superClass.prototype.constructor === Object.prototype.constructor){
superClass.prototype.constructor = superClass;
}
}
使用:
function Author(name,books){
Author.superClass.constructor.call(this.name);
this.books = books;
}
extends(Author,Person);
Author.prototype.getBooks = function(){
return this.books;
}
本文介绍了一种在JavaScript中实现继承的方法,通过构造函数和原型链来扩展一个子类的功能并保留父类的特性。示例展示了如何创建Person基类,并通过自定义的extend函数创建Author子类,同时继承Person类的属性。

被折叠的 条评论
为什么被折叠?



