4 JavaScript primer, part 2

 

This chapter covers

  • Working with JavaScript object prototypes
  • Defining JavaScript classes
  • Generating and consuming sequences
  • Using JavaScript collections
  • Creating and using JavaScript modules

In this chapter, I continue describing the JavaScript features that are important to TypeScript development. I focus on the JavaScript support for objects, the different ways they can be defined, and how they relate to JavaScript classes. I also demonstrate the features for handling sequences of values, the JavaScript collections, and the modules feature, which allows a project to be split up into multiple JavaScript files.

4.1 Preparing for this chapter

In this chapter, I continue to use the primer project created in chapter 3. To prepare for this chapter, replace the contents of the index.js file in the primer folder with the code shown in listing 4.1.

Tip

You can download the example project for this chapter—and for all the other chapters in this book—from https://github.com/manningbooks/essential-typescript-5.

Listing 4.1 Replacing the code in the index.js file in the primer folder
let hat = {
   name: "Hat",    
   price: 100,
   getPriceIncTax() {
       return Number(this.price) * 1.2;
   }    
};

console.log(`Hat: ${hat.price}, ${hat.getPriceIncTax() }`);

Open a new command prompt, navigate to the primer folder, and run the command shown in listing 4.2 to start monitoring and executing the JavaScript file.

4.2 Understanding JavaScript object inheritance

4.2.1 Inspecting and modifying an object’s prototype

4.2.2 Creating custom prototypes

4.2.3 Using constructor functions

4.2.4 Chaining constructor functions

4.2.5 Checking prototype types

4.2.6 Defining static properties and methods

4.2.7 Using JavaScript classes

4.3 Using iterators and generators

4.3.1 Using a generator

4.3.2 Defining iterable objects

4.4 Using JavaScript collections

4.4.1 Storing data by key using an object

4.4.2 Storing data by key using a map

4.4.3 Using symbols for map keys