Javascript继承Demo

代码
<html>
<script>
function Person(name){
this.name=name;
}
Person.prototype.getName
=function(){
return this.name;
}
function Author(name, books) {
Person.call(
this, name); // Call the superclass's constructor in the scope of this.
this.books = books; // Add an attribute to Author.
}
Author.prototype
= new Person(); // Set up the prototype chain.
Author.prototype.constructor = Author; // Set the constructor attribute to Author.
Author.prototype.getBooks = function() { // Add a method to Author.
return this.books;
};
var author = [];
author[
0] = new Author('Dustin Diaz', ['JavaScript Design Patterns']);
author[
1] = new Author('Ross Harmes', ['JavaScript Design Patterns']);
alert(author[
1].getName());
alert(author[
1].getBooks());
</script>
</html>

 

posted @ 2010-06-24 18:34  chunchill  阅读(188)  评论(0编辑  收藏  举报