<script type="text/javascript">
function Person(name) {
this.name = name;
} Person.prototype.getName = function() {
return this.name;
} function Author(name, books) {
Person.call(this, name); // 定义:调用一个对象的一个方法,以另一个对象替换当前对象。
this.books = books; // Add an attribute to Author.
} Author.prototype = new Person(); // 设置原型链
Author.prototype.constructor = Author; // 设置构造属性
Author.prototype.getBooks = function() { // 添加方法
return this.books;
}; var author = [];
author[] = new Author('Dustin Diaz', ['JavaScript Design Patterns']);
author[] = new Author('Ross Harmes', ['JavaScript Design Patterns']); alert(author[].getName()); //输出 Dustin Diaz
alert(author[].getBooks()); //输出 JavaScript Design Patterns
alert(author[].getName()); //输出 Ross Harmes
alert(author[].getBooks()); //输出 JavaScript Design Patterns
</script>

功力不够,无法理解

进一步升级提取

<script type="text/javascript">
/* 扩展函数 */
function extend(subClass, superClass) {
var F = function() {};
F.prototype = superClass.prototype; // F已成superClass父类
subClass.prototype = new F(); //子类继承父类的原子
subClass.prototype.constructor = subClass;
} /* Person类 */ function Person(name) {
this.name = name;
} Person.prototype.getName = function() {
return this.name;
} /* Author类 */ function Author(name, books) {
Person.call(this, name);
this.books = books;
}
extend(Author, Person); Author.prototype.getBooks = function() {
return this.books;
}; var author = []; //定义数组
author[] = new Author('Dustin Diaz', ['JavaScript Design Patterns']);
author[] = new Author('Ross Harmes', ['JavaScript Design Patterns']); alert(author[].getName()); //输出 Dustin Diaz
alert(author[].getBooks()); //输出 JavaScript Design Patterns
alert(author[].getName()); //输出 Ross Harmes
alert(author[].getBooks()); //输出 JavaScript Design Patterns
</script>

进一步改进,太牛逼了,作者

<script type="text/javascript">
/* 扩展函数 */
function extend(subClass, superClass) {
var F = function() {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass; subClass.superclass = superClass.prototype;
if(superClass.prototype.constructor == Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
}
} /* Person类 */ function Person(name) {
this.name = name;
} Person.prototype.getName = function() {
return this.name;
} /* Author类 */ function Author(name, books) {
Author.superclass.constructor.call(this, name);
this.books = books;
}
extend(Author, Person); Author.prototype.getBooks = function() {
return this.books;
}; Author.prototype.getName = function() {
var name = Author.superclass.getName.call(this);
return name + ', Author of ' + this.getBooks().join(', ');
}; var author = []; //定义数组
author[] = new Author('Dustin Diaz', ['JavaScript Design Patterns']);
author[] = new Author('Ross Harmes', ['JavaScript Design Patterns']); alert(author[].getName()); //输出 Dustin Diaz , Author of JavaScript Design Patterns
alert(author[].getBooks()); //输出 JavaScript Design Patterns
alert(author[].getName()); //输出 Ross Harmes , Author of JavaScript Design Patterns
alert(author[].getBooks()); //输出 JavaScript Design Patterns
</script>

js深入研究之无法理解的js类代码,extend扩展的更多相关文章

  1. js深入研究之神奇的匿名函数类生成方式

    <script type="text/javascript"> var Book = (function() { // 私有静态属性 ; // 私有静态方法 funct ...

  2. 深入理解unslider.js源码

    最近用到了一个挺好用的幻灯片插件,叫做unslider.js,就想看看怎么实现幻灯片功能,就看看源码,顺便自己也学习学习.看完之后收获很多,这里和大家分享一下. unslider.js 源码和使用教程 ...

  3. Js 职责链模式 简单理解

    js 职责链模式 的简单理解.大叔的代码太高深了,不好理解. function Handler(s) { this.successor = s || null; this.handle = funct ...

  4. JS魔法堂:彻底理解0.1 + 0.2 === 0.30000000000000004的背后

    Brief 一天有个朋友问我“JS中计算0.7 * 180怎么会等于125.99999999998,坑也太多了吧!”那时我猜测是二进制表示数值时发生round-off error所导致,但并不清楚具体 ...

  5. 关于闭包的理解(JS学习小结)

    前言: 啊啊啊,看书真的很痛苦啊,还是好想做项目写代码才有意思,不过我现在缺的确是将知识体系化,所以不论看书多么痛苦都一定要坚持坚持啊,这才是我现在最需要的进步的地方,加油! 因为现在期末啦,下周一也 ...

  6. 封装常用的js(Base.js)——【01】理解库,获取节点,连缀,

    封装常用的js(Base.js)——[01]理解库,获取节点,连缀,  youjobit07 2014-10-10 15:32:59 前言:       现如今有太多优秀的开源javascript库, ...

  7. 关于js with语句的一些理解

    关于js with语句的一些理解   今天看到js的with语句部分,书中写到,with语句接收的对象会添加到作用域链的前端并在代码执行完之后移除.看到这里,我有两点疑问,添加到作用域链前端是不是指对 ...

  8. 【翻译】要理解Ext JS 5小工具

    原版的:Understanding Widgets in Ext JS 5 在Ext JS 5,引入了新的"widgetcolumn",支持在网格的单元格中放置组件. 同一时候,还 ...

  9. 理解Node.js的事件轮询

    前言 总括 : 原文地址:理解Node.js的事件轮询 Node小应用:Node-sample 智者阅读群书,亦阅历人生 正文 Node.js的两个基本概念 Node.js的第一个基本概念就是I/O操 ...

随机推荐

  1. Hadoop-2.x的源码编译

    由于在Hadoop-2.x中,Apache官网上提供的都是32位版本,如果是生产环境中则需要自行编译64位,编译Hadoop-2.x版本方法如下: 安装编译源码所依赖的底层库 yum install ...

  2. N - Picture - poj 1177(扫描线求周长)

    题意:求周长的,把矩形先进行融合后的周长,包括内周长 分析:刚看的时候感觉会跟棘手,让人无从下手,不过学过扫描线之后相信就很简单了吧(扫描线的模板- -),还是不说了,下面是一精确图,可以拿来调试数据 ...

  3. mongodb授权登录

    mongodb版本为3.2(目前最新),演示的是linux下的mongodb授权认证 第一次登录不启动授权(mongo默认不启动) ./mongod --dbpath=/home/db/data -- ...

  4. mac下Apache添加限速模块mod_bw

    官方文档: Apache2 - Mod_bw v0.7 Author : Ivan Barrera A. (Bruce) HomePage : Http://Ivn.cl/apache & h ...

  5. FilterDispatcher 的作用(struts2.1.3以前,新版本改了)

    org.apache.struts2.dispatcher.FilterDispatcher是Struts2的主要的Filter,负责四个方面的功能: (1)执行Actions (2)清除Action ...

  6. 基于年纪和成本(Age & Cost)的缓存替换(cache replacement)机制

    一.客户端的缓存与缓存替换机制 客户端的资源缓存: 在客户端游戏中,通常有大量的资源要处理,这些可能包括贴图.动作.模型.特效等等,这些资源往往存在着磁盘文件->内存(->显存)的数据通路 ...

  7. tstring

      是的,一旦知道 TCHAR 和_T 是如何工作的,那么这个问题很简单.基本思想是 TCHAR 要么是char,要么是 wchar_t,这取决于 _UNICODE 的值: // abridged f ...

  8. Calendar 日历控件使用

    <link rel="stylesheet" href="__STATIC__/js/calendar/calendar-blue.css"/> & ...

  9. mybati的存储过程

    这里我就以的存储过程为例,大家一起学习一下,

  10. Draggable(拖动)组件

    一.加载方式 //class 加载方式 <div id="box" class="easyui-draggable" style="width: ...