关于javascript继承的疑惑
以下是学习中遇到的问题,望大神给个解释,先谢谢啦!
首先是clone函数:
function clone(object){ function F(){} F.prototype = object; return new F();}var Person = { name : "maoyuanjun", getName : function(){ return this.name; }}var Author = clone(Person);Author.books = [];/*[color=#FF0000]//这样子声明是错误的Author.prototype.getBooks = function(){ return this.books;}*/[/color][color=#FF0000]//这样子声明是正确的呢?Author.getBooks = function(){ return this.books;}[/color]var author = [];author[0] = clone(Author);author[0].name = "myj";author[0].books = ["java"];author[1] = clone(Author);author[1].name = "zlp";author[1].books = ["javascript"];alert(author[0].getName() + "," + author[0].getBooks());alert(author[1].getName() + "," + author[1].getBooks());Author.prototype.getBooks = function(){ return this.books;}