首页 > Web开发 > 详细

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

时间:2014-04-02 12:58:07      阅读:542      评论:0      收藏:0      [点我收藏+]



/* 基类*/

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();


js面向对象编程,一个完整原型的继承例子,布布扣,bubuko.com

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

原文:http://blog.csdn.net/xuexiaodong009/article/details/21096817

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!