js深入研究之无法理解的js类代码,extend扩展
<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扩展的更多相关文章
- js深入研究之神奇的匿名函数类生成方式
<script type="text/javascript"> var Book = (function() { // 私有静态属性 ; // 私有静态方法 funct ...
- 深入理解unslider.js源码
最近用到了一个挺好用的幻灯片插件,叫做unslider.js,就想看看怎么实现幻灯片功能,就看看源码,顺便自己也学习学习.看完之后收获很多,这里和大家分享一下. unslider.js 源码和使用教程 ...
- Js 职责链模式 简单理解
js 职责链模式 的简单理解.大叔的代码太高深了,不好理解. function Handler(s) { this.successor = s || null; this.handle = funct ...
- JS魔法堂:彻底理解0.1 + 0.2 === 0.30000000000000004的背后
Brief 一天有个朋友问我“JS中计算0.7 * 180怎么会等于125.99999999998,坑也太多了吧!”那时我猜测是二进制表示数值时发生round-off error所导致,但并不清楚具体 ...
- 关于闭包的理解(JS学习小结)
前言: 啊啊啊,看书真的很痛苦啊,还是好想做项目写代码才有意思,不过我现在缺的确是将知识体系化,所以不论看书多么痛苦都一定要坚持坚持啊,这才是我现在最需要的进步的地方,加油! 因为现在期末啦,下周一也 ...
- 封装常用的js(Base.js)——【01】理解库,获取节点,连缀,
封装常用的js(Base.js)——[01]理解库,获取节点,连缀, youjobit07 2014-10-10 15:32:59 前言: 现如今有太多优秀的开源javascript库, ...
- 关于js with语句的一些理解
关于js with语句的一些理解 今天看到js的with语句部分,书中写到,with语句接收的对象会添加到作用域链的前端并在代码执行完之后移除.看到这里,我有两点疑问,添加到作用域链前端是不是指对 ...
- 【翻译】要理解Ext JS 5小工具
原版的:Understanding Widgets in Ext JS 5 在Ext JS 5,引入了新的"widgetcolumn",支持在网格的单元格中放置组件. 同一时候,还 ...
- 理解Node.js的事件轮询
前言 总括 : 原文地址:理解Node.js的事件轮询 Node小应用:Node-sample 智者阅读群书,亦阅历人生 正文 Node.js的两个基本概念 Node.js的第一个基本概念就是I/O操 ...
随机推荐
- ubuntu下安装pdo扩展
ubuntu下安装好LAMP后默认情况没有安装mysql_pdo扩展,以下是安装 步聚,在终端输入以下命令 1.pecl search pdo 2.sudo pecl install pdo 当出现E ...
- Beanstalkd介绍
特征 优先级:任务 (job) 可以有 0~2^32 个优先级, 0 代表最高优先级,beanstalkd 采用最大最小堆 (Min-max heap) 处理任务优先级排序, 任何时刻调用 reser ...
- wpf下拉框不能多选的原因
<dxe:ComboBoxEdit Margin="0" Height="25" Width="65" VerticalAlignm ...
- 设置UIScrollView只可以水平或者竖直滚动
UIScrollView里边包含多个UIWebView: 可以通过设置contentSize的值,设置其width为UIScrollerView可视区域的宽度:即UIScrollView的width, ...
- Linux(CentOS 5.5) Redis安装
一,什么是redis redis是一个key-value存储系统. 和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset ...
- [Angular 2] Simple intro Http
To use http, we need to import the HTTP_PROVIDER, so that we can inject http to other component: imp ...
- js控制select数据绑定下拉列表
JS代码段: <script type="text/javascript"> $(document).ready(function () { $("sele ...
- Qt install Phonon
sudo apt-get install libphonon-dev phonon-backend-gstreamer
- 使用Ksoap2调用Web Service加入SoapHeader
关于这个问题,如果使用百度都是前篇一律的代码,好不容易上了google才找到完整的方法,这里讲所有的代码都贴出来与大家分享. 首先是.NET写的后台代码 /// <summary> /// ...
- window成员和document成员
输出浏览器成员和DOM成员(以下为safari浏览器测试)(浏览器不同对象成员有差异) window成员 <script type="text/javascript"> ...