您的位置:首页 > Web前端 > JavaScript

js面向对象编程,一个完整原型的继承例子

2014-04-02 09:30 656 查看
/* 基类*/

var Person = {
name: 'default name',
getName: function() {
return this.name;
}
};


公共方法

function clone(object) {
function F() {}
F.prototype = object;
return new F;
}

/* 子类1*/
var reader = clone(Person);
alert(reader.getName()); // This will output 'default name'.
reader.name = 'John Smith';
alert(reader.getName()); // This will now output 'John Smith'.

/* 子类1*/

var Author = clone(Person);
Author.books = []; // Default value.
Author.getBooks = function() {
return this.books;
}

var author = [];
/* 定义实例*/
author[0] = clone(Author);
author[0].name = 'Dustin Diaz';
author[0].books = ['JavaScript Design Patterns'];

author[1] = clone(Author);
author[1].name = 'Ross Harmes';
author[1].books = ['JavaScript Design Patterns'];

author[1].getName();
author[1].getBooks();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: