现在的位置: 首页 > 综合 > 正文

Javascript继承Demo

2012年07月19日 ⁄ 综合 ⁄ 共 663字 ⁄ 字号 评论关闭
代码

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

 

抱歉!评论已关闭.