惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
小众软件
小众软件
Y
Y Combinator Blog
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LangChain Blog
Martin Fowler
Martin Fowler
美团技术团队
B
Blog RSS Feed
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
博客园 - 司徒正美
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
Hugging Face - Blog
Hugging Face - Blog
D
Docker
G
Google Developers Blog

博客园 - 小郝(Kaibo Hao)

Big O Complexity Chart (From: bigocheatsheet.com) Machine Learning Note - Note 1 JavaScript Patterns 7.1 Singleton JavaScript Patterns 6.7 Borrowing Methods JavaScript Patterns 6.6 Mix-ins JavaScript Patterns 6.5 Inheritance by Copying Properties JavaScript Patterns 6.3 Klass JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns JavaScript Patterns 5.9 method() Method JavaScript Patterns 5.8 Chaining Pattern JavaScript Patterns 5.7 Object Constants JavaScript Patterns 5.6 Static Members JavaScript Patterns 5.5 Sandbox Pattern JavaScript Patterns 5.4 Module Pattern JavaScript Patterns 5.3 Private Properties and Methods JavaScript Patterns 5.2 Declaring Dependencies JavaScript Patterns 5.1 Namespace Pattern JavaScript Patterns 4.10 Curry
JavaScript Patterns 6.4 Prototypal Inheritance
小郝(Kaibo Hao) · 2014-07-18 · via 博客园 - 小郝(Kaibo Hao)

2014-07-18 23:42  小郝(Kaibo Hao)  阅读(347)  评论()    收藏  举报

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)