The Principles of Object-Oriented JavaScript
About This Book
If you've used a more traditional object-oriented language, such as C++ or Java, JavaScript probably doesn't seem object-oriented at all. It has no concept of classes, and you don’t even need to define any objects in order to write code. But don’t be fooled—JavaScript is an incredibly powerful and expressive object-oriented language that puts many design decisions right into your hands.
In The Principles of Object-Oriented JavaScript, Nicholas C. 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 primitive and reference values
* What makes JavaScript functions so unique
* The various ways to create objects
* How to define your own constructors
* How to work with and understand prototypes
* Inheritance patterns for types and objects
The Principles of Object-Oriented JavaScript will leave even experienced developers with a deeper understanding of JavaScript. Unlock the secrets behind how objects work in JavaScript so you can write clearer, more flexible, and more efficient code.
Use the new ES2015 setter and getter methods. Also
super()
has to be the first method call in the constructor method.Here is a great video tutorial about ES2015 Classes: https://www.youtube.com/watch?v=EUtZRwA7Fqc
I also recommend "The Principles of Object-Oriented JavaScript " by Nicholas C. Zakas https://www.amazon.com/Principles-Object-Oriented-JavaScript-Nicholas-Zakas/dp/1593275404
Members bound to
Prototype
are shared across instances. There is only one instance of that member.Members set directly to
this
inside the constructor or set to a specific instance ofPoint
is available to that specific instance ofPoint
only. If you want to expose logic shared across all instances ofPoint
, it's best to set it on thePrototype
.The cool part is that you can always access the
prototype
even if you have declared it on the instance. If so, pass the instance as context:The above snippet illustrates fundamental concepts of JavaScript (the context) and object-oriented JavaScript (prototype). If the subject is of interest to you, take a look at N. Zackas' book on the subject: https://www.amazon.com/Principles-Object-Oriented-JavaScript-Nicholas-Zakas/dp/1593275404