`

<<javascript:the good part>> Inheritance

 
阅读更多

Javascript does not have the concept of class. It is a prototyal language which means objects inherits directly from other projects.

The book introduces several ways to implement the inheritance.

1. Pseudoclassical

2. Prototypal

3. Functional

4. Parts

 

Below, I give an example of Functional inheritance, which I think is the most "javascript way".

function parent(s) {
    var name = s.name;
    var o={};
    o.getName = function(){
        return name;
    };
    return o;
}

var p=parent({name:"zhangruimin"});
console.log(p.getName());

function child(s) {
    var title = s.title; //private field
    var o=parent(s); //like call the parent constructor
    var getParentName=o.getName;// store to implement override
    var getTitle= function(){// private function
         return title;
    }
    o.getName = function(){
        return "In child: "+ getTitle()+getParentName();
    };
    return o;
}

var c=child({name:"zhangruimin", title:"MR."});
console.log(c.getName());
 

Here, we've got:

1. inheritance

2. private function and field

3. Call parent method

分享到:
评论

相关推荐

    JavaScript: The Good Parts

    Most programming languages contain good and bad parts, but JavaScript has more than its share of the bad, having been developed and released in a hurry before it could be refined. This authoritative ...

    《windows mobile 手机应用开发》源码

    创建窗体并管理多个窗体程序的范例源代码&lt;br&gt;WeatherGage: 一个自定义基类窗体的范例源代码&lt;br&gt;Inheritance: 从一个自定义父类窗体继承创建窗体的范例源代码&lt;br&gt;&lt;br&gt;第14章&lt;br&gt;DrawRectangles: 使用触笔绘制矩形...

    JavaScript The Good Parts

    JavaScript The Good Parts Chapter 1. Good Parts Chapter 2. Grammar Chapter 3. Objects Chapter 4. Functions Chapter 5. Inheritance Chapter 6. Arrays Chapter 7. Regular Expressions Chapter 8. Methods ...

    JavaScript.Object.Programming.148421

    This brief book explains the advantages of the object model, inheritance, both classical and prototypical, and shows how these concepts can be implemented in JavaScript. It also shows how object ...

    Exploring C++ 11

    Part 1: The Basics -The Map Data Structure Part 1: The Basics -Type Synonyms Part 1: The Basics -Characters Part 1: The Basics -Character Categories Part 1: The Basics -Case-Folding Part 1: The Basics...

    Test-Driven JavaScript Development

    If you’re already well-versed in advanced JavaScript concepts such as closures, prototypal inheritance, the dynamic nature of this, and feature detection, you may want to skim this part for a ...

    Inside the C++ Object Model

    oriented&lt;br&gt;programming within C++: constructor semantics, temporary generation, support for encapsulation,&lt;br&gt;inheritance, and "the virtuals"-virtual functions and virtual inheritance. This book ...

    Type.Inheritance.and.Relational.Theory

    However, there doesn’t seem to be any consensus in the community at large on a formal, rigorous, and abstract model of inheritance. This book proposes such a model, one that enjoys several ...

    The Principles of Object-Oriented JavaScript 1st Edition

    Zakas thoroughly explores JavaScript's object-oriented nature, revealing the language's unique implementation of inheritance and other key characteristics. You'll learn: –The difference between ...

    Object Pascal Handbook,最新Ddelphi书籍,for XE7

    Chapter 8: Inheritance Chapter 9: Handling Exceptions Chapter 10: Properties and Events Chapter 11: Interfaces Chapter 12: Manipulating Classes Chapter 13: Objects and Memory Part III Chapter 14: ...

    Ehlib_6.1.129

    The history of the EhLib library -------------------------------- Version 6.1 + A new component TDBVertGridEh - DataSet Grid with the ... &lt;EhLib Archive&gt; \ Demos \ Bin \ DBGridEh.LoadingStatus.Exe

    JavaScript Data Structures and Algorithms

    JavaScript follows the prototypal inheritance pattern, unlike Java and C++ (which follow the inheritance pattern), there are some changes in writing data structures in JavaScript. The classical ...

    Ehlib6.1.129

    The history of the EhLib library -------------------------------- Version 6.1 + A new component TDBVertGridEh - DataSet Grid with the ... &lt;EhLib Archive&gt; \ Demos \ Bin \ DBGridEh.LoadingStatus.Exe

    Advanced JavaScript (closures,prototype,inheritance)

    NULL 博文链接:https://butterflymacro.iteye.com/blog/1271789

    Packt.Object-Oriented.JavaScript.3rd.Edition

    the new features added in ES6 Find out about ECMAScript 6’s Arrow functions, and make them your own Understand objects in Google Chrome developer tools and how to use Them Use a mix of prototypal ...

    Speaking JavaScript

    Like it or not, JavaScript is everywhere these days—from browser to server to mobile—and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and ...

    Javascript.Object.Oriented.Programming.pdf

    Build sophisticated web applications by mastering the art of Object-Oriented Javascript About This Book Learn popular Object-Oriented programming (OOP) principles and design patterns to build robust ...

    Wiley.Publishing.Professional.C++.2005.pdf

    Part II: C++ Coding the Professional Way Chapter 7: Coding with Style Chapters 8 and 9: Classes and Objects Chapter 10: Discovering Inheritance Techniques Chapter 11: Writing Generic Code with ...

    Csharp.6.0.and.the.NET.4.6.Framework.7th.Edition.1484213335.epub

    This new 7th edition of Pro C# 6.0 and the .NET 4.6 Platform has been completely revised and rewritten to reflect the latest changes to the C# language specification and new advances in the .NET ...

    javax.persistence.jar

    javax.persistence.Inheritance.class javax.persistence.InheritanceType.class javax.persistence.JoinColumn.class javax.persistence.JoinColumns.class javax.persistence.JoinTable.class javax.persistence....

Global site tag (gtag.js) - Google Analytics