The Principles of Object-Oriented JavaScript

Category: Programming
Author: Nicholas C. Zakas
4.8
This Year Stack Overflow 1
This Month Stack Overflow 2

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.

Comments

by anonymous   2019-07-21

Use the new ES2015 setter and getter methods. Also super() has to be the first method call in the constructor method.

class Circle {
  constructor(cx) {
    this.cx = cx;
    …
  }

  get cx() {
    return this.cx;
  }

  set cx(cx) {
    this.cx = cx;
  }

}

class RandomCircle extends Circle {
  constructor() {
    super();
    this.cx = random(10);
  }

  random(n) {
    …
  }
}

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

by anonymous   2017-08-20

Members bound to Prototype are shared across instances. There is only one instance of that member.

function Point(value) { this.value = value; }
Point.prototype.moveBy = function() { console.log('from the prototype', this.value);};
var p1 = new Point('p1');
var p2 = new Point('p2');
p1.moveBy === p2.moveBy; //true. Same instance of moveBy function

Members set directly to this inside the constructor or set to a specific instance of Point is available to that specific instance of Point only. If you want to expose logic shared across all instances of Point, it's best to set it on the Prototype.

var p3 = new Point('p3');
p3.moveBy = function() { console.log('from the instance', this.value); };
p3.moveBy === p2.moveBy; //false

var p4 = new Point('p4');
p4.moveBy = function() { console.log('from the instance', this.value) };
p4.moveBy === p3.moveBy; //false

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:

Point.prototype.moveBy.call(p3); //will call moveBy set on the prototype. Output: "from the prototype p3"
p3.moveBy(); //will call moveBy set on the instance. Output: from the instance p3

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

by narak   2017-08-19
Would also recommend "Principles of Object Oriented JavaScript" by him: https://www.amazon.com/Principles-Object-Oriented-JavaScript...