Intermediate JavaScript Basics - Classes to Modules
1. Intro to Classes
//Example: Template for dog behaviour
class Dog {
constructor(name) {
this._name = name;
this._behavior = 0;
}
get name() {
return this._name;
}
get behavior() {
return this._behavior;
}
incrementBehavior() {
this._behavior ++;
}
}
const halley = new Dog('Halley');
console.log(halley.name); // Print name value to console
console.log(halley.behavior); // Print behavior value to console
halley.incrementBehavior(); // Add one to behavior
console.log(halley.name); // Print name value to console
console.log(halley.behavior); // Print behavior value to consoleyou created a class called
Dog, and used it to produce aDogobject.there are similarities between class and object syntax, but JavaScript calls the
constructor()method every time it creates a new instance of a class.
# Explanation of the syntax
Dogis the name of our class. By convention, we capitalize and CamelCase class names.JavaScript will invoke the
constructor()method every time we create a new instance of ourDogclass.This
constructor()method accepts one argument,name.Inside of the
constructor()method, we use thethiskeyword. In the context of a class,thisrefers to an instance of that class. In theDogclass, we usethisto set the value of the Dog instance’snameproperty to thenameargument.Under
this.name, we create a property calledbehavior, which will keep track of the number of times a dog misbehaves. Thebehaviorproperty is always initialized to zero.
2. Instances
Now, we’re ready to create class instances. An instance is an object that contains the property names and methods of a class, but with unique property values.
Below our Dog class, we use the new keyword to create an instance of our Dog class.
store the instance in a variable
halleythat will store an instance of ourDogclass.We use the
newkeyword to generate a new instance of theDogclass. Thenewkeyword calls theconstructor(), runs the code inside of it, and then returns the new instance.We pass the
'Halley'string to theDogconstructor, which sets thenameproperty to'Halley'.Finally, we log the value saved to the
namekey in ourhalleyobject, which logs'Halley'to the console.
3. Methods
Class method and getter syntax is the same as it is for objects except you can not include commas between methods
In the example above, we add getter methods for name and behavior. Notice, we also prepended our property names with underscores (_name and _behavior), which indicate these properties should not be accessed directly. Under the getters, we add a method named .incrementBehavior(). When you call .incrementBehavior() on a Dog instance, it adds 1 to the _behavior property. Between each of our methods, we did not include commas.
Case 1 : Surgery Department Management
Method Calls
The syntax for calling methods and getters on an instance is the same as calling them on an object — append the instance with a period, then the property or method name. For methods, you must also include opening and closing parentheses. instance.method()
4. Inheritance
New keywords: extends and super

The
extendskeyword makes the methods of the animal class available inside the cat class.The constructor, called when you create a new
Catobject, accepts two arguments,nameandusesLitter.The
superkeyword calls the constructor of the parent class. In this case,super(name)passes the name argument of theCatclass to the constructor of theAnimalclass. When theAnimalconstructor runs, it setsthis._name = name;for newCatinstances._usesLitteris a new property that is unique to theCatclass, so we set it in theCatconstructor.
Case 2 : Surgery Department & Getter
5. Static Methods
Sometimes you will want a class to have methods that aren’t available in individual instances, but that you can call directly from the class. Because of the static keyword, we can only access .generateName() by appending it to the Animal class.
The static keyword defines a static method or property for a class, or a class static initialization block (see the link for more information about this usage). Neither static methods nor static properties can be called on instances of the class. Instead, they're called on the class itself.
Static methods are often utility functions, such as functions to create or clone objects, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.
We call the .generateName() method with the following syntax:
You cannot access the .generateName() method from instances of the Animal class or instances of its subclasses. The example above will result in an error, because you cannot call static methods (.generateName()) on an instance (tyson).
Case 3 : Password Generator for Hospital Employees using Static Method
6. Modules
# Intro to Runtime Environment
A runtime environment is where your program will be executed. JavaScript code may be executed in one of two runtime environments:
a browser’s runtime environment
the Node runtime environment
In each of these environments, different data values and functions are available, and these differences help distinguish front-end applications from back-end applications.
Front-end JavaScript applications are executed in a browser’s runtime environment and have access to the
windowobject.Back-end JavaScript applications are executed in the Node runtime environment and have access to the file system, databases, and networks attached to the server.
Modules are reusable pieces of code in a file that can be exported and then imported for use in another file. A modular program is one whose components can be separated, used individually, and recombined to create a complex system.
# Module Implementation
In JavaScript, there are two runtime environments and each has a preferred module implementation:
The Node runtime environment and the
module.exportsandrequire()syntax.The browser’s runtime environment and the ES6
import/exportsyntax.
# Module.exports
To create a module, you simply have to create a new file where the functions can be declared. Then, to make these functions available to other files, add them as properties to the built-in module.exports object
# Require
The require() function accepts a string as an argument. That string provides the file path to the module you would like to import. When you use require(), the entire module.exports object is returned and stored in the variable converters. This means that both the .celsiusToFahrenheit() and .fahrenheitToCelsius() methods can be used in this program!
Coding Challenges - Modules Concept Review 1
7. Review of Modules - Import & Export for ES6
Exporting in 2 ways:
Importing:
Remember to adjust HTML for importing module:
ES6 Modules Code Challenge #2:
Renaming exports from diff modules to avoid naming collisions

Project : Workaround Explorer (To do)
29 Nov 2021 5pm
Last updated