JavaScript – Classes and Modules

How to install docker on windows 10″ href=”” target=”_blank”>How to install docker on windows 10

JavaScript objects were covered in Chapter 6.
That chapter treated each object as a unique set of properties,
different from every other object. It is often useful, however, to
define a class of objects that share certain
properties. Members, or instances, of the class
have their own properties to hold or define their state, but they also
have properties (typically methods) that define their behavior. This
behavior is defined by the class and is shared by all instances. Imagine
a class named Complex to represent and perform arithmetic on complex
numbers, for example. A Complex instance would have properties to hold
the real and imaginary parts (state) of the complex number. And the
Complex class would define methods to perform addition and
multiplication (behavior) of those numbers.

In JavaScript, classes are based on JavaScript’s prototype-based
inheritance mechanism. If two objects inherit properties from the same
prototype object, then we say that they are instances of the same class.
JavaScript prototypes and inheritance were covered in Prototypes and Inheritance, and you must
be familiar with the material in those sections to understand this
chapter. This chapter covers prototypes in Classes and Prototypes.

If two objects inherit from the same prototype, this typically
(but not necessarily) means that they were created and initialized by
the same constructor function. Constructors have been covered in Object Creation Expressions, Creating Objects with new, and
Constructor Invocation, and this chapter has more in
Classes and Constructors.

If you’re familiar with strongly-typed object-oriented programming
languages like Java or C++, you’ll notice that JavaScript classes are
quite different from classes in those languages. There are some
syntactic similarities, and you can emulate many features of “classical”
classes in JavaScript, but it is best to understand up front that
JavaScript’s classes and prototype-based inheritance mechanism are
substantially different from the classes and class-based inheritance
mechanism of Java and similar languages. Java-Style Classes in JavaScript demonstrates classical classes in

One of the important features of JavaScript classes is that they
are dynamically extendable. Augmenting Classes
explains how to do this. Classes can be thought of as types, and Classes and Types explains several ways to test or determine
the class of an object. That section also covers a programming
philosophy known as “duck-typing” that de-emphasizes object type in
favor of object capability.

After covering all of these fundamentals of object-oriented
programming in JavaScript, the chapter shifts to more practical and less
architectural matters. Object-Oriented Techniques in JavaScript includes two
nontrivial example classes and demonstrates a number of practical
object-oriented techniques for improving those classes. Subclasses demonstrates (with many examples) how to extend
or subclass other classes and how to define class hierarchies in
JavaScript. Classes in ECMAScript 5 covers some of the things
you can do with classes using the new features of ECMAScript

Defining classes is a way of writing modular, reusable code, and
the last section of this chapter talks about JavaScript modules more

Comments are closed.