JavaScript Object Programming (eBook)
XIX, 109 Seiten
Apress (Verlag)
978-1-4842-1787-0 (ISBN)
- What are objects, JavaScript objects and object programming
- What is and how to use inheritance and JavaScript inheritance as well as inheritance alternatives
- How to design for JavaScript
- What are and how to use OO principles in JavaScript
- How to use Constructors with JavaScript and more
Martin Rinehart, a self-confessed JavaScript lover, set aside work on his five-volume frontend-engineering textbook project for long enough to write this small book on JavaScript inheritance. He wanted to eliminate some of the massive confusion surrounding this important subject. Martin is the author of over a dozen books on programming, and of the JSWindows system that brings a windowing UI to browser-based applications.
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 programming (OP) opens a new world of design possibilities that go far beyond inheritance.This book will help the intermediate JavaScript programmer learn to use both types of inheritance. For classical inheritance, it is accompanied by a substantial online system (a windowing UI library) that shows classical inheritance at its best. The same system shows how OP "e;capabilities"e; can eliminate much of the need for inheritance. For experienced JavaScript programmers, this book shows why most of the old views of JavaScript's inheritance have not done it justice. JavaScript classes inherit from JavaScript's prototypes, a fact that makes JavaScript's prototypes, when used correctly, functional equivalents to C++ classes(not to prototypes in true prototypical languages, like Self). JavaScript's object programming (not inheritance) is what separates it from classical OOP languages like C++ and Java. Most important, basing inheritance on JavaScript's prototypal chain is possible, but is not the best choice for prototypal inheritance or classical inheritance.What You'll LearnWhat are objects, JavaScript objects and object programmingWhat is and how to use inheritance and JavaScript inheritance as well as inheritance alternativesHow to design for JavaScriptWhat are and how to use OO principles in JavaScriptHow to use Constructors with JavaScript and moreAudienceThis book is for both intermediate and advanced JavaScript and Web development programmers. However, any programmer will understand the concepts and any JavaScript programmer should understand all of the concepts in this book. The code there is shows examples of the concepts discussed.
Martin Rinehart, a self-confessed JavaScript lover, set aside work on his five-volume frontend-engineering textbook project for long enough to write this small book on JavaScript inheritance. He wanted to eliminate some of the massive confusion surrounding this important subject. Martin is the author of over a dozen books on programming, and of the JSWindows system that brings a windowing UI to browser-based applications.
Contents at a Glance 6
Contents 7
About the Author 14
A Note for the Implementers 15
Introduction 16
Chapter 1: Creating Objects 17
Reasons for Objects 17
Objects Do Methods 17
Event-Driven Programming 18
Taming Exponential Complexity 18
Class-Based vs. Prototypal 18
Simula 18
Smalltalk 18
C++ and Java 19
Self and JavaScript 19
Objects Up Close 20
Data Properties 20
Methods (Code Properties) 20
Ex Nihilo Object Creation 21
The Object Constructor 21
Object Literals 22
More Ex Nihilo Objects 23
OOP-Style Object Creation 24
Constructors 24
Assigning Initial Property Values 24
Creating Instance Methods 25
Creating Class Statics 25
Getters and Setters 25
Default Values 26
Prototypal Object Creation 26
Object Prototypes 26
The Prototype Chain 28
Object Prototype Cloning 28
Summary 30
Chapter 2: Object Programming 31
JSWindows Sample System 31
OP Removes Restrictions 31
OP Defined 32
Programming with Properties 32
Dot Notation 32
Subscript Notation 33
Object Programming Examples 33
Object Sum 33
OP for Inheriting Prototypes 35
OP in the JSWindows Library 35
DOM Related 35
Utility 37
Summary 40
Chapter 3: Inheritance Theory 41
Classes 41
Constructors 42
Instance Methods 42
Class (Family-Wide) Properties 43
Data 43
Methods 43
Class-Based Inheritance 43
Property Sets 44
Constructing an Extending Instance 45
Overriding Properties 45
Inheritance Chains 46
Prototypal Inheritance 47
Inheritance vs. Composition 48
Composition in Theory 48
Composition in JSWindows 48
Complex Properties 49
Behaviors 50
Summary 51
Chapter 4: Inheritance Practice 52
Cascading init() Methods for Data 52
A Theoretical Example 54
The call() and apply() Methods 56
Extending an Extending Family 56
A Practical Example 57
Discrete Defaults 58
Implementing Capabilities 59
Prototypes for Methods 59
Theory 60
Prototypal Inheritance 60
Prototype Inheritance Alternatives 61
Prototype Alternatives 63
Library Functions 63
Prototype Lookups and Performance 63
Class (Family-Wide) Methods 64
JSWindows Inheritance 64
Summary 64
Chapter 5: On OOP Principles 65
Ranking OOP Principles 65
Inheritance 66
Encapsulation 66
Access Specifiers 66
Closures 67
Polymorphism 67
Subtype Polymorphism 67
Parametric Polymorphism 68
Ad Hoc and Other Polymorphism 69
JavaScript and Polymorphism 69
Classes, Abstraction, and Interfaces 70
Classes 70
Abstraction 71
Interfaces 71
Other OOP Principles 72
Summary 72
Chapter 6: More Ex Nihilo Objects 73
The Ex Nihilo Namespace Object 73
The Ex Nihilo Class 74
Returning Ex Nihilo Objects 75
The Function as an Ex Nihilo Class 75
Summary 76
Chapter 7: Inheritance Alternatives 78
Multiple Inheritance 78
Interfaces 80
Capabilities 81
The Window[_M[_BS]] Problem 81
Mixins 83
Calling Capability Methods 84
Capabilities as Constructor Properties 84
Capabilities as Single Properties 84
Capability Prototype Methods 85
Examples 85
Closable 86
Maskable 87
Button_sizable 89
The Button_sizable Constructor 90
The Click Functions 90
Choosable Buttons 91
The Button_sizable.Button Family 92
The Button_sizable.panel Family 93
The Click and Draw Functions 93
Summary 94
Chapter 8: Designing for JavaScript 96
Use Ex Nihilo Constantly 96
Array Literals 96
Styles Objects 97
Other Objects 97
Use Composition Liberally 97
Original Pos_size 98
Mature Pos_size 98
Use Capabilities Liberally 99
Use Inheritance Conservatively 100
Summary 101
Chapter 9: On Constructors 102
Constructor Magic 102
The new Operator 102
The this Parameter 103
The constructor.prototype 103
The “[[prototype]]” Property 104
The Prototype’s Prototype 105
“[[prototype]]” Implies 105
The Dynamic Prototype 106
A Bit More Magic 106
The Constructor Returns this 106
The “Magic” Summarized 107
Constructors Are Not for Inheritance 107
Summary 108
Chapter 10: Appendices 109
A Surveyed Pages, OOP Principles 109
B Selected Books 110
C++ 110
Java 111
JavaScript 111
Python 111
Visual Basic 111
C Selected Websites 111
Wikipedia on Object-Oriented Programming, Class-Based Inheritance and Prototypal Inheritance 111
The Author’s Web Site on Class-Based Inheritance and JavaScript Programming 112
Other Web Sites on Class-Based Inheritance and Prototypal Inheritance 112
Object-Oriented Programming: 112
Prototype-Based Programming 112
Classes 113
Instances 113
Methods 113
Inheritance 113
Composition 114
D Defined Terms 114
E Support for Selected Statements 114
F Simple Closure 115
G Sealing and Freezing Objects 116
H Configuring Properties 117
I Dynamic Properties and Me 118
Index 119
Erscheint lt. Verlag | 30.12.2015 |
---|---|
Zusatzinfo | XIX, 109 p. 14 illus. |
Verlagsort | Berkeley |
Sprache | englisch |
Themenwelt | Mathematik / Informatik ► Informatik ► Programmiersprachen / -werkzeuge |
Mathematik / Informatik ► Informatik ► Software Entwicklung | |
Mathematik / Informatik ► Informatik ► Web / Internet | |
Schlagworte | constructor • Inheritance • JavaScript • object programming • objects • OO • programming • Prototype • Web Development |
ISBN-10 | 1-4842-1787-X / 148421787X |
ISBN-13 | 978-1-4842-1787-0 / 9781484217870 |
Haben Sie eine Frage zum Produkt? |
Größe: 1,6 MB
DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasserzeichen und ist damit für Sie personalisiert. Bei einer missbräuchlichen Weitergabe des eBooks an Dritte ist eine Rückverfolgung an die Quelle möglich.
Dateiformat: PDF (Portable Document Format)
Mit einem festen Seitenlayout eignet sich die PDF besonders für Fachbücher mit Spalten, Tabellen und Abbildungen. Eine PDF kann auf fast allen Geräten angezeigt werden, ist aber für kleine Displays (Smartphone, eReader) nur eingeschränkt geeignet.
Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.
Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.
Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.
aus dem Bereich