<script>
function Person(name){
this.name=name;
}
Person.prototype.getName=function(){
return this.name;
}
//var person = new Person("代绍帅");
//alert(person.getName());
/**************Author继承Person类***************/
/****创建构造函数 ****/
function Author(names,books){
/****调用超类的构造函数***/
Person.call(this, names);
this.books = books;
}
/******将子类指向超类的一个实例*/
Author.prototype = new Person();
/*****prototype设置为Person的实例时,其Constructor属性会被抹掉*/
Author.prototype.constructor = Author;
Author.prototype.getBooks = function(){
return this.books;
}
var author = new Author("代绍帅", "Cbooks");
alert(author.getName());
alert(author.getBooks());
</script>
Extend Demo:
<script>
var Person = {
name:'default Name',
getName:function(){
return this.name;
}
};
function clone(object){
function F(){}
F.prototype = object;
return new F;
}
var reader = clone(Person);
var readerwang = clone(Person);
reader.name='aa';
alert(reader.getName());
alert(readerwang.getName());
</script>
个人见解:
个人感觉每一次所谓的继承,重载基本都是通过三个步骤:
1.模拟javascript new 新建对象时,建立空对象。
2.将空对象的prototype只想superclass。
3.重新将subclass的constructor指向subclass。