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.