javascript语言精粹:继承
继承提供了2个有用的任务:
1.代码重用
2.引入了一套类型系统的规范,因为程序员无需编写显示类型转换的代码,他们的工作量将大大减轻。这是一件很好的事情,应为类型转换会丧失类型系统在安全上的优势。
在基于类的语言中,对象是类的实例,并且类可以从另一个类继承,javascript是一门基于原型的语言,这意味着对象直接可以从其他对象继承。
伪类:
JavaScript is conflicted about its prototypal nature. Its prototype mechanism is
obscured by some complicated syntactic business that looks vaguely classical.
Instead of having objects inherit directly from other objects, an unnecessary level of
indirection is inserted such that objects are produced by constructor functions.
When a function object is created, the Function constructor that produces the function
object runs some code like this:
它不直接让对象从其他对象继承,反而插入了一个多余 的间接层:通过构造器函数产生对象。当一个函数
对象被创建时,Function构造器产生的函数对象会运行类似这样的一些代码:
this.prototype = {constructor: this};
The new function object is given a prototype property whose value is an object containing
a constructor property whose value is the new function object. The
prototype object is the place where inherited traits are to be deposited. Every function
gets a prototype object because the language does not provide a way of determining
which functions are intended to be used as constructors. The constructor
property is not useful. It is the prototype object that is important.
When a function is invoked with the constructor invocation pattern using the new
prefix, this modifies the way in which the function is executed. If the new operator
were a method instead of an operator, it could have been implemented like this:
(注:method方法如下:
Function.prototype.method=function(name,func){
if(!this.prototype[name])
this.prototype[name]=func;
return this;
};
通过给Functon.protoype增加一个method方法,我们下次给对象增加方法时就不用键入prototype这几个字符,省掉了一点麻烦)
javascript语言精粹:继承的更多相关文章
- JavaScript语言精粹 笔记03 继承
继承伪类对象说明符原型函数化部件 继承 JS不是基于类的,而是基于原型的,这意味着对象直接从其他对象继承. 1 伪类 JS提供了一套丰富的代码重用模式,它可以模拟那些基于类的模式,因为JS实际上没有类 ...
- 《JavaScript语言精粹》小记
一.前言 以下内容均摘自<JavaScript语言精粹>一书,本人在读这本书时,发现作者诠释JavaScript很犀利,特别是数组部分,固记录下来,想和大家分享下. 随笔主要包含两大部分: ...
- javascript语言精粹
内容选自:<javascript语言精粹> 1.6种值会为假(==false),分别是false,null,undefined,' ',0,NaN 2.typeof有6种值,分别是'num ...
- Javascript 语言精粹 代码片段合集
Javascript 语言精粹 代码片段合集 标签:Douglas-Crockford Javascript 最佳实践 原文链接 更好的阅读体验 使用一个method 方法定义新方法 Function ...
- JavaScript语言精粹笔记
JavaScript语言精粹笔记 掌握语言的每个特性可以让你出风头,但是并不推荐,因为一部分的特性带来的麻烦可能远超本身的价值.正如书中所言,坏的材料并不能雕刻出好的作品,要成为一名更好的程序员,要取 ...
- 《JavaScript语言精粹》之函数化
写在前面 看到好多书评和读书笔记都说<JavaScript语言精粹>字字珠玑,名不虚传..当然,要看得懂才行 其实个人认为函数化部分不是很好,举的例子不是十分恰当,之前看不懂是因为被成功误 ...
- 读《JavaScript语言精粹》的一些感言
最近看了<JavaScript语言精粹>,并且连着看了两遍,如果非要用言语形容的话,那我只能用4个字来形容:相见恨晚.其中的一些经验经过这么多年的摸索其实也了然,但是作者用这么浅薄的书把有 ...
- 《JavaScript语言精粹》【PDF】下载
<JavaScript语言精粹>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382204 内容简介 javascript曾是&q ...
- JavaScript语言精粹 笔记04 数组
数组1 数组字面量2 长度3 删除4 列举5 混淆的地方6 方法7 维度 数组1 数组字面量 var empty = []; var numbers = [ 'zero', 'one', 'two', ...
随机推荐
- elaserch 查看节点是否是master
http://192.168.32.81:9200/_cat/nodes 192.168.32.81 192.168.32.81 3 21 0.00 d * node02 192.168.32.80 ...
- 不同服务器之间使用svn钩子post-commit同步代码遇到的证书认证问题.md
遇到的问题,以下其他问题都是因解决这个问题引申出来的问题 VisualSVN hooks自动同步更新到web服务器 错误信息如下: Error validating server certificat ...
- ajenti试用感受
ajenti试用感受_展现技术动态_百度空间 ajenti试用感受 今天看开源中国介绍一款名为"服务器管理系统"的开源软件发布了,名为ajenti,页面感觉不错,对机器的采集信 ...
- poj 3335 Rotating Scoreboard - 半平面交
/* poj 3335 Rotating Scoreboard - 半平面交 点是顺时针给出的 */ #include <stdio.h> #include<math.h> c ...
- ASP.NET 母版页和内容页的加载顺序
Master 模板页Content 内容页如果希望Master页面的数据传给Content页面,请Init如果希望Content页面的数据传给Master页面,请重载Load具体细节不多说了,看下面页 ...
- ASP.NET Calendar 控件
ASP.NET Calendar 控件 http://www.w3school.com.cn/aspnet/control_calendar.asp
- cookie保存分页参数
<script src="../Public/js/jquery.cookie.js"></script> <script type="te ...
- Linux学习之查找命令find
1.find -name 根据名称查找文件 *通配所有字符: ?统配单个字符 2.find -iname 根据名称不区分大小写 3.find -size 根据文件大小 find / -size + ...
- 通过代码...CGRectmake 创建的控件如何自适应屏幕。
[[UIScreen mainScreen] bounds].size.width [[UIScreen mainScreen] bounds].size.height 得到屏幕的宽高,然后将变量写入 ...
- this class is not key value coding-compliant for the key detailItem
我出这个错误是因为,自己的一个视图没有给指定想用的viewController文件..及一个classController控制一个xib文件,忘给该视图指定控制器了.