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的更多相关文章

  1. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  2. 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 ...

  3. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  4. JavaScript Patterns 6.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

  5. 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 ...

  6. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  7. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  8. JavaScript Patterns 5.9 method() Method

    Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...

  9. JavaScript Patterns 5.8 Chaining Pattern

    Chaining Pattern - Call methods on an object one after the other without assigning the return values ...

随机推荐

  1. QTableWidget 使用及美化_QtableWidget_QtableView滚动条宽度及样式

      //创建及属性设置m_tableWidget = new QTableWidget(this);m_tableWidget->setRowCount(10);m_tableWidget-&g ...

  2. 【Java每日一题】20161019

    20161018问题解析请点击今日问题下方的"[Java每日一题]20161019"查看 package Oct2016; import java.util.List; publi ...

  3. Rest.Ler PHP API Server解决方案

    https://github.com/Luracast/Restler 通过composer安装依赖后,整个vendor将近三十兆.这太不爽了.搞PHP的人如果没有洁癖的追求,跟搞Java的咸鱼有神码 ...

  4. 应用服务器和Web服务器

    如上图所示,绝大部分的公司会采用Apache+tomcat集群(或jetty集群)来部署公司的Web服务, Web服务器和应用服务器关系,先介绍一下我们常说的服务器: Tomcat服务器,是运行ser ...

  5. Java分布式开发

    分布式概念的引入是基于性能的提升,应用的可靠性而提出的.所谓Java分布式,即是在使用Java语言进行企业级应用开发的过程中,采用分布式技术解决业务逻辑的高并发.高可用性的一些架构设计方案. 1. R ...

  6. [moka同学笔记]Yii下国家省市三级联动

    第一次做省市三级联动时候遇到了坑,感觉还是自己太菜.头疼了很久研究了很久,最后终于发现了问题.大致总结一下思路 在控制器中实例化model,然后在视图中渲染所有国家,当选取国家时候,ajax通过 id ...

  7. android 歌词解析

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...

  8. Python中类的定义

    class Student(object): # 有点类似其它高级语言的构造函数 def __init__(self,name,score): self.name = name self.score ...

  9. [转载]协程-cooperative multitasking

    [转载]协程三讲 http://ravenw.com/blog/2011/08/24/coroutine-part-1-defination-and-classification-of-corouti ...

  10. .Net加密保护工具分析介绍

    本文主要介绍一些dotNet加密保护工具的原理以及就其脱壳进行简单探讨. remotesoft protector.maxtocode..Net Reactor.Cliprotector.themid ...