JavaScript-mancy: Object-Oriented Programming
JavaScript-mancy: Object-Oriented Programming
Mastering the Arcane Art of Summoning Objects in JavaScript for C# Developers
About the Book
This is the second book of the JavaScript-mancy series. Within its mysterious pages you'll find:
- A great introduction to OOP in JavaScript full of fantasy inspired examples and exercises
- A detailed look at the pillars of OOP in JavaScript adapted for developers with a C# background
- An awesome walkthrough of how to achieve classical inheritance in JavaScript with and without ES6 classes
- A step beyond classical inheritance into new OOP paradigms that take advantage of the dynamic nature of JavaScript to bring super flexibility and composability into your apps
- A look into the obscure object internals and meta-programming with the new Reflect API, Proxies and Symbols
- A thorough introduction to TypeScript and how it can boost your developer productivity
- A lot of references to C# that will make it super easy for C# and Java developers to wrap their heads around JavaScript and its quirks. (This also extends to developers familiar with strongly typed languages)
- A breathtaking story of EPIC proportions and awkward sense of humor...
Imagine... imagine you lived in a world were you could use JavaScript to change the universe around you, to tamper with the threads that compose reality as you know it and do anything that you can imagine. Well, welcome to the world of JavaScript-mancy, where wizards, also known as JavaScriptmancers, control the arcane winds of magic wielding JavaScript to and fro and command the very fabric of reality.
We, programmers, sadly do not live in such a world. But we do have a measure of magic in ourselves, we have the skills and power to create things out of nothingness. And even if we cannot throw fireballs or levitate (yet), we can definitely change/improve/enhance reality and the universe around us with our little creations. Ain't that freaking awesome?
Well, I hope this book inspires you to continue creating, and using this beautiful skill we share, this time, with JavaScript ;).
For many years JavaScript has been frowned upon and looked down on by many developers due to its quirky nature, obscure behaviors and many WTFs that populate its hairy APIs and operations.
Frown upon no more! For with modern design patterns, libraries, tools and the long awaited ECMAScript 6 (ES6, ES2015) writing JavaScript is now a pleasure.
Join me at the school of JavaScript-mancy as we travel along the modern landscape of writing JavaScript in 2015 and beyond, as we discover the organic evolution of this beautiful language and its thriving ecosystem, and delve in the latest features/spells/incantations of the JavaScript Arcana.
You my friend, can no longer ignore JavaScript. JavaScript is the most deployed language on earth, a beautiful and expressive language that supports many paradigms and which has a thriving community that continuously expands and improves its ecosystem with patterns, libraries, frameworks and tools. You don't want to miss this train.
But JavaScript, though forgiving and specially conceived to be easy to learn, can be either daunting for us that have a strongly-typed mindset and come from languages such as C# or Java or, more often, laughed at as a toy.
For you who consider it daunting and hate working with it worry not! I will show you the most common misconceptions and all the patterns you need to know to become as fluent in JavaScript as you are in C#.
For you who consider it a toy language, something associated not with real programming but with copy-paste coding or scripting to add flare to websites, I will show you all the different patterns and programming paradigms that JavaScript supports and which make it just as good and powerful as C#.
Let's get our JavaScript adventure started!
Bundles that include this book
About the Contributors

Technical Reviewer
Artur is a passionate software developer who has built various web applications for small as well as enterprise companies.
Sometimes he recollects the good, old times when jQuery was in beta, just about to be released as 1.0 and nobody even knew what SPA stood for... Everyday he tries to get better with modern front- end development and software craftsmanship.
Currently he works as Senior Applications Developer at Oracle. When he gets home he plays around with side projects, open source or gets outside and does some running.
Reader Testimonials

Matt Ruby
Principal Software Engineer at Bluestem Brands, Inc.
This has been the best book I've read on OOP in JS. Most books that cover this subject use obtuse examples. Jaime's examples cover common cases and how to solve them while embracing javascript instead of fighting it. I've been using js for years and I've learned many new techniques from this book. I highly recommend it!

Kári Tristan Helgason
Software Engineer at Google and Technical Reviewer for this book
This book will not only teach you things you didn't know about object-oriented programming in JavaScript, it will also keep you entertained while doing so! Even if you have no interest in object-oriented programming, and have years of experience with JavaScript, I can guarantee that you will learn new patterns and ways of thinking about a problem from this book. Jaime is fantastic at breaking down concepts and exploring them from different angles until they fall into place. I really enjoyed it.
Table of Contents
-
- About The Author
- About the Technical Reviewers
-
Prelude
- A Note to the Illustrious Readers of JavaScript-mancy: Getting Started
- A Story About Why I Wrote This Book
- Why Should You Care About JavaScript?
- What is the Goal of This Book?
- What is the Goal of The JavaScript-mancy Series?
- Why JavaScript-mancy?
- Is This Book For You?
- How is The Book Organized?
- How Are The JavaScript-mancy Series Organized? What is There in the Rest of the Books?
- Understanding the Code Samples in This Book
- A Note About ECMAScript 5 (ES5) and ES6, ES7, ES8 and ESnext within The Book
-
A Note Regarding the Use of
var
,let
andconst
- A Note About the Use of Generalizations in This Book
- Do You Have Any Feedback? Found Any Error?
- A Final Word From the Author
- Once Upon a Time…
-
Tome II. JavaScriptmancy and OOP: The Path of The Summoner
-
Introduction to the Path of Summoning and Commanding Objects (aka OOP)
- Let me Tell You About OOP in JavaScript
- C# Classes in JavaScript
- OOP Beyond Classes
- Combining Classes with Object Composition
- The Path of the Object Summoner Step by Step
- Concluding
-
Summoning Fundamentals: Encapsulation and Information Hiding
- Let’s get Started With The Basics of OOP!
- Encapsulation: Creating Objects in JavaScript
- Object Initializers
- Constructor Functions and the New Operator
- Data Hiding in JavaScript
- Object Initializers vs Constructor Functions
- Object Factories vs Constructor Functions
- Concluding
- Exercises
-
Summoning Fundamentals: Prototypical Inheritance
- You Don’t Repeat Yourself. Inheritance!
- Classical Inheritance vs Prototypical Inheritance
- JavaScript Prototypical Inheritance
- Object Prototypes
- Object Prototypes with Object.Create or OLOO
- Defining Prototypes with Constructor Functions
- Creating Longer Prototype Chains
- What About Concatenative Protypical Inheritance?
- Object Initializers vs Object.create vs Constructor Functions
- Concluding
- Exercises
-
Summoning Fundamentals: Polymorphism
- Polymorphism Means Many Forms
- Polymorphism in C#
- Polymorphism in JavaScript
- Concluding
- Exercises
-
White Tower Summoning: Mimicking C# Classical Inheritance in JavaScript
- Ever Heard of Classical Inheritance?
- Emulating a C# Class in JavaScript
- Constructor Function + Prototype = Class
- Mimicking Classical Inheritance in JavaScript
- Simplifying Classical Inheritance in ES5
- Concluding
- Exercises
-
White Tower Summoning Enhanced: The Marvels of ES6 Classes
- Create These Units Faster with ES6 Classes!
- From ES5 “Classes” to ES6 Classes
- Prototypical Inheritance via Extends
- Overriding Methods in ES6 Classes
- Static Members and Methods
- ES6 Classes and Information Hiding
- ES6 Classes Behind the Curtain
- Concluding
- Exercises
-
Black Tower Summoning: Objects Interweaving Objects with Mixins
- The Problem With Classes and Classical Inheritance…
- Free Yourself From Classes With Object Composition and Mixins
- Limitations of Mixins as Objects
- Functional Mixins
- Combining Mixins with ES6 Classes
- Object.assign in Depth
- Object.assign Alternatives for ES5 JavaScript-mancers
- Concluding
- Exercises
-
Black Tower Summoning: Safer Object Composition with Traits
- An Improvement Over Mixins
- Traits
- Traits with traits.js
- Composing Traits
- What Happens When You Miss Required Properties?
- Resolving Name Conflicts
- Traits and Data Privacy
- High Integrity Objects With Immutable Traits
- Traits vs Mixins
- Concluding
- Exercises
-
Black Tower Summoning: Next Level Object Composition With Stamps
- I Call Them Stamps
- What are Stamps?
- Stamps OOP Embraces JavaScript
- Stamps By Example
- Stamp Composition
- Stamp Fluent API
- Concluding: Stamps vs Mixins vs Traits
- Exercises
-
Object Internals: The Secrets of Objects
- A Nifty Trick… Object Internals
-
All your Objects Are Belong to
Object
- Defining Properties with Object.defineProperty
- Defining Multiple Properties with Object.defineProperties
- Beautiful Property Manipulation with ESnext Decorators
- Class And Method Decorators
- Create Objects With Object.create And Property Descriptors
- Metaprogramming
- Other Useful Object Methods
- Concluding
- Exercises
-
More Metaprogramming with Reflect, Proxies and Symbols
- How Good Are You at Reflection?
- ES6 Reflect
- Reflection? What is reflection?
- ES6 Proxies
- ES6 Symbols and Meta-programming
- Concluding
- Exercises
-
TypeScript
- You Shall Only Use Types!
- JavaScript + Types = Awesome Dev Productivity
- Any JavaScript is Valid TypeScript
- So, What Are The Advantages and Disadvantages of TypeScript?
- Setting up a Simple TypeScript project
- Cool TypeScript Features
- Type Annotations In TypeScript
- Working with TypeScript in Real World Applications
- Concluding
- Exercises
-
Introduction to the Path of Summoning and Commanding Objects (aka OOP)
-
Tome II. Epilogue
- Thank you!
-
References and Appendix
-
Appendix A. On the Art of Summoning Servants and Critters, Or Understanding The Basics of JavaScript Objects
- An Army of Objects
- Object Initializers (a.k.a. Object Literals)
- Creating Objects With Factories
- Data Privacy in JavaScript
- ES6 Improves Object Initializers
- ES6 Symbols and Data Privacy
- Concluding
- Exercises
-
Appendix B. Mysteries of the JavaScript Arcana
- A Couple of Tips About JavaScript Quirks and Gotchas
- A Quick Refresher of the JavaScript Arcana 101
- This, Your Most Dangerous Foe
- Global Scope by Default and Namespacing in JavaScript
- Type Coercion Madness
- Using JavaScript in Strict Mode
- Concluding
- Exercises
-
Appendix C. More Useful Function Patterns: Function Overloading
- Have you Heard About The Marvels Of Overloading?
- The Problem with Function Overloading in JavaScript
- How Do We Do Function Overloading Then?
- Function Overloading by Inspecting Arguments
- Using an Options Object
- Relying on ES6 Defaults
- Taking Advantage of Polymorphic Functions
- Concluding
- Exercises
-
Appendix D. Setting Up Your Developing Environment For ES6
- Using ES6 with Node.js
- ES6 and Modern Browsers
- Real-World ES6 Development Environments
- Appendix E. Fantasy Glossary
-
References
- Specifications
- Books
- White papers
- Articles
-
Appendix A. On the Art of Summoning Servants and Critters, Or Understanding The Basics of JavaScript Objects
- Notes
Other books by this author
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
Do Well. Do Good.
Authors have earned$11,597,977writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets) and MOBI (for Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Top Books
C++20 - The Complete Guide
Nicolai M. JosuttisAll new language and library features of C++20 (for those who know previous C++ versions).
The book presents all new language and library features of C++20. Learn how this impacts day-to-day programming, to benefit in practice, to combine new features, and to avoid all new traps.
Buy early, pay less, free updates.
Other books:
Jetpack Compose internals
Jorge CastilloJetpack Compose is the future of Android UI. Master how it works internally and become a more efficient developer with it. You'll also find it valuable if you are not an Android dev. This book provides all the details to understand how the Compose compiler & runtime work, and how to create a client library using them.
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
SignalR on .NET 6 - the Complete Guide
Fiodar SazanavetsLearn everything there is to learn about SignalR and how to integrate it with the latest .NET 6 and C# 10 features. Learn how to connect any type of client to SignalR, including plain WebSocket client. Learn how to build interactive applications that can communicate with each other in real time without making excessive calls.
R Programming for Data Science
Roger D. PengThis book brings the fundamentals of R programming to you, using the same material developed as part of the industry-leading Johns Hopkins Data Science Specialization. The skills taught in this book will lay the foundation for you to begin your journey learning data science. Printed copies of this book are available through Lulu.
Functional event-driven architecture: Powered by Scala 3
Gabriel VolpeExplore the event-driven architecture (EDA) in a purely functional way, mainly powered by Fs2 streams in Scala 3!
Leverage your functional programming skills by designing and writing stateless microservices that scale, powered by stateful message brokers.
The easiest way to learn design patterns
Fiodar SazanavetsLearn design patterns in the easiest way possible. You will no longer have to brute-force your way through each one of them while trying to figure out how it works. The book provides a unique methodology that will make your understanding of design patterns stick. It can also be used as a reference book where you can find design patterns in seconds.
Cronache di Domain-Driven Design
Francesco Strazzullo, Matteo Baglini, Gianluca Padovani, Emanuele DelBono, Marco Consolaro, Alessandro Colla, Uberto Barbini, Alberto Acerbis, Julie Camosseto, DDD Open, and Alberto BrandoliniCronache di Domain-Driven Design: un libro corale in italiano fatto di storie indipendenti tra loro, che sono il risultato dell’applicazione di Domain-Driven Design su progetti reali.
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
CCIE Service Provider Version 4 Written and Lab Exam Comprehensive Guide
Nicholas RussoThe service provider landscape has changed rapidly over the past several years. Networking vendors are continuing to propose new standards, techniques, and procedures for overcoming new challenges while concurrently reducing costs and delivering new services. Cisco has recently updated the CCIE Service Provider track to reflect these changes; this book represents the author's personal journey in achieving that certification.
Top Bundles
- #1
Software Architecture for Developers: Volumes 1 & 2 - Technical leadership and communication
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #2
Practical FP in Scala + Functional event-driven architecture
2 Books
Practical FP in Scala (A hands-on approach) & Functional event-driven architecture, aka FEDA, (Powered by Scala 3), together as a bundle! The content of PFP in Scala is a requirement to understand FEDA so why not take advantage of this bundle!? - #3
All the Books of The Medical Futurist
6 Books
We put together the most popular books from The Medical Futurist to provide a clear picture about the major trends shaping the future of medicine and healthcare. Digital health technologies, artificial intelligence, the future of 20 medical specialties, big pharma, data privacy, digital health investments and how technology giants such as Amazon... - #4
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #6
Pattern-Oriented Memory Forensics and Malware Detection
2 Books
This training bundle for security engineers and researchers, malware and memory forensics analysts includes two accelerated training courses for Windows memory dump analysis using WinDbg. It is also useful for technical support and escalation engineers who analyze memory dumps from complex software environments and need to check for possible... - #7
Modern C++ Collection
3 Books
Get All about Modern C++C++ Standard Library, including C++20Concurrency with Modern C++, including C++20C++20Each book has about 200 complete code examples. Updates are included. When I update one of the books, you immediately get the updated bundle. You can expect significant updates to each new C++ standard (C++23, C++26, .. ) and also... - #9
Retromat eBook Bundle for Agile Retrospectives
2 Books
If you facilitate retrospectives this bundle is for you: "Plans for Retrospectives" helps beginners learn the lay of the land with tried-and-true plans. Once you know your way around, "Run great agile retrospectives" contains all 135+ activities in Retromat for you to mix and match.