JavaScript Patterns 6.4 Prototypal Inheritance
No classes involved; Objects inherit from other objects.
Use an empty temporary constructor function F(). Set the prototype of F() to be the parent object. Return a new instance of the temporary constructor.
function Object(o) { function F() {} F.prototype = o; return new F(); } // object to inherit from var parent = { name: "Papa" }; // the new object var child = Object(parent); // testing alert(child.name); // "Papa"
Addition to ECMAScript 5
In ECMAScript 5, the prototypal inheritance pattern becomes officially a part of the language. This pattern is implemented through the method Object.create().
var child = Object.create(parent);
Object.create()accepts an additional parameter, an object. The properties of the extra object will be added as own properties of the new child object being returned.
var child = Object.create(parent, { age: { value: 2 } // ECMA5 descriptor }); child.hasOwnProperty("age"); // true
References:
JavaScript Patterns - by Stoyan Stefanov (O`Reilly)
JavaScript Patterns 6.4 Prototypal Inheritance的更多相关文章
- JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance
// the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...
- JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns
In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var ada ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
- JavaScript Patterns 6.3 Klass
Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...
- JavaScript Patterns 7.1 Singleton
7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...
- JavaScript Patterns 6.7 Borrowing Methods
Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...
- JavaScript Patterns 6.6 Mix-ins
Loop through arguments and copy every property of every object passed to the function. And the resul ...
- JavaScript Patterns 5.9 method() Method
Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...
- JavaScript Patterns 5.8 Chaining Pattern
Chaining Pattern - Call methods on an object one after the other without assigning the return values ...
随机推荐
- SignalR入门之基本介绍
SignalR是微软对web开发技术的扩充,它是一种框架,方便用来创建实时应用程序. 有一些即时消息系统,实时协作工作集,多人在线游戏,信息广播服务等其他需要在信息产生时就能进行发送的任务系统. 对于 ...
- Liskov替换原则
一.定义 子类型必须能替换掉它们的基类型 二.提取公共部分的方法代替继承 如果一组类都支持一个公共的职责,那么它们应该从一个公共的超类继承该职责. 如果公共的超类不存在,那么就创建一个,并把公共的职责 ...
- 制作Java视频播放器
一.工程准备 首先需要下载并安装VLC播放器,然后下载vlcj外部库. 下载slf4j中的slf4j-api-1.7.13.jar. slf4j- nop-1.7.13.jar 将vlc安装目录下的l ...
- Spring注解@Value的用法
有时候我们在配置文件中使用配置的信息不仅需要在xml文件中使用,还可能在类中使用,这个时候,我们可使用@Value注解了: @Value("${rest.service.url}" ...
- 解决ambiguous symbol命名空间中类名、变量名冲突的问题
最近在将一个复杂的工程集成到现有的项目中.编译时发现,有的变量名冲突了,提示就是xxxx ambiguous symbol,并且在编译输出时,指明了两个文件当中特定的变量名或者类名相同.出现这个编译错 ...
- 免安装的tomcat双击startup.bat后,启动窗口一闪而过,而且tomcat服务未启动。
免安装的tomcat双击startup.bat后,启动窗口一闪而过,而且tomcat服务未启动. 原因是:在启动tomcat是,需要读取环境变量和配置信息,缺少了这些信息,就不能登记环境变量,导致了t ...
- Verilog学习笔记简单功能实现(八)...............异步FIFO
基本原理: 1.读写指针的工作原理 写指针:总是指向下一个将要被写入的单元,复位时,指向第1个单元(编号为0). 读指针:总是指向当前要被读出的数据,复位时,指向第1个单元(编号为0). ...
- log4j2配置详解
1. log4j2需要两个jar log4j-api-2.x.x.jar log4j-core-2.x.x.jar .log4j和log4j2有很大的区别,jar包不要应错. 2. ...
- Bootstrap源码分析之transition、affix
一.Transition(过滤) 作为一个基础支持的组件,被其他组件多次引用.实现根据浏览器支持transition的能力,然后绑定动画的结束事件:首先:创建一个Element:然后:迭代查看此元素支 ...
- angular源码分析:angular源代码的获取与编译环境安装
一.安装git客户端 1.windows环境推荐使用TortoiseGit. 官网地址:http://tortoisegit.org 下载地址:http://tortoisegit.org/downl ...