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 ...
随机推荐
- ajax携带状态值
- Java并发编程:并发容器之ConcurrentHashMap(转载)
Java并发编程:并发容器之ConcurrentHashMap(转载) 下面这部分内容转载自: http://www.haogongju.net/art/2350374 JDK5中添加了新的concu ...
- Guava学习笔记:Ordering犀利的比较器
Ordering是Guava类库提供的一个犀利强大的比较器工具,Guava的Ordering和JDK Comparator相比功能更强.它非常容易扩展,可以轻松构造复杂的comparator,然后用在 ...
- MyBatis入门(一)
一.MyBaris简介 1)MyBaris发展过程 MyBatis的前身叫iBatis,本是apache的一个开源项目, 2010年这个项目由apache software foundation 迁移 ...
- SSH免密码登录过程解析和实现
SSH 为建立在应用层和传输层基础上的安全协议.SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议.利用SSH 协议可以有效防止远程管理过程中的信息泄露问题. 从客户端来看,SSH提 ...
- java对cookie的操作
java对cookie的操作比较简单,主要介绍下建立cookie和读取cookie,以及如何设定cookie的生命周期和cookie的路径问题. 建立一个无生命周期的cookie,即随着浏览器的关闭即 ...
- The template engine
Play has an efficient templating system which allows to dynamically generate HTML, XML, JSON or any ...
- Photoshop如何实现UI自动切图?
切图严格来说并不是UI设计师的工作, 而是前端工程师的工作,指的是将UI设计师的设计(大部分为photoshop创建的PSD文件)转化为界面(网页或窗体等)所需要资源的过程.切图是衔接UI设计和应用程 ...
- 你会喜欢的25个创意的扁平化 LOGO 设计
扁平设计的风暴席卷了整个设计领域,它不仅影响网页设计,也影响了用户界面和标志设计.最近,我们看到了很多大公司,如谷歌和必应开始使用扁平设计的标识. 一个 LOGO 应该简单,显眼和精美,以适应产品的整 ...
- Shepherd – 在应用程序中轻松实现引导功能
Shepherd 是一个指导用户使用应用程序的 JavaScript 库.它使用 Tether——另一个开源库,实现所有的步骤.Tether 确保你的步骤不会溢出屏幕或被剪裁.你可以很容易地指导用户使 ...