Node.js Tutorial - 22 - Extending from EventEmitter

Codevolution
23 Dec 202208:01

Summary

TLDRThis video tutorial introduces how to build a custom module in Node.js that extends the EventEmitter class. The presenter demonstrates creating a 'pizzashop.js' file with a class that handles order placements and displays the order number. By inheriting from EventEmitter, the PizzaShop class can emit custom events, which are then listened to and handled in 'index.js'. The video also shows how to integrate the event-driven architecture with other modules, such as a 'drinkmachine.js', to create a loosely coupled system. The presenter encourages viewers to learn more about class inheritance in JavaScript and hints at upcoming videos on built-in modules like FS, streams, and HTTP.

Takeaways

  • 📚 The video introduces creating a custom module in Node.js that builds on the Event Emitter class.
  • 🍕 A 'pizzashop.js' file is created to define a PizzaShop class with a constructor and methods for order handling.
  • 🔢 The PizzaShop class initializes an order number to zero and includes methods to increment and display the order number.
  • 📦 The PizzaShop class is exported for use in other modules, demonstrating modularity in Node.js applications.
  • 📝 In 'index.js', the PizzaShop class is imported and an instance is created to test the order methods.
  • 🔄 The video explains how to use event-driven architecture in the PizzaShop class by utilizing inheritance from the Event Emitter class.
  • 📜 The 'extends' keyword is used to inherit from the Event Emitter, allowing the PizzaShop class to emit events.
  • 📡 The 'this.emit' method is used within the order method to emit an 'order' event with 'size' and 'Topping' parameters.
  • 📌 Event listeners are attached in 'index.js' to handle the 'order' event and log the received order details to the console.
  • 🥤 A separate module, 'drinkmachine.js', is introduced to handle drink serving logic based on the order size.
  • 🔗 The DrinkMachine class is used within the order event listener to serve a complimentary drink for large orders, showcasing event-based interaction between modules.

Q & A

  • What is the main focus of the video?

    -The video focuses on demonstrating how to create a custom module in Node.js that builds on top of the event emitter class, using the example of a pizza shop.

  • What is the purpose of creating a 'pizzashop.js' file?

    -The 'pizzashop.js' file is created to define a PizzaShop class that has its own properties and methods for handling orders in an event-driven architecture.

  • What are the two methods defined in the PizzaShop class?

    -The two methods defined in the PizzaShop class are one to place an order, which increments the order number, and another called displayOrderNumber to view the current order number.

  • How is the PizzaShop class exported for use in other modules?

    -The PizzaShop class is exported using the `module.exports` syntax, allowing it to be imported and used in other modules like 'index.js'.

  • What is the purpose of the 'extends' keyword in JavaScript?

    -The 'extends' keyword in JavaScript is used for inheritance, allowing one class to inherit the properties and methods of another class.

  • How does the PizzaShop class inherit from the event emitter class?

    -The PizzaShop class inherits from the event emitter class by using the `extends` keyword and invoking the `super` function within its constructor.

  • What is the significance of emitting an event in the order method?

    -Emitting an event in the order method allows the PizzaShop class to trigger custom events, such as 'order', and pass parameters like size and toppings to event listeners.

  • What is the role of the 'DrinkMachine' class in the script?

    -The 'DrinkMachine' class is a separate module that handles the logic for serving drinks based on the order size, and it is used to demonstrate how different modules can interact using events.

  • How does the 'DrinkMachine' class interact with the PizzaShop class?

    -The 'DrinkMachine' class interacts with the PizzaShop class by being called within the order event listener, where it receives the order size and decides whether to serve a complimentary drink.

  • What is the advantage of using event-driven architecture in this scenario?

    -The advantage of using event-driven architecture is that it allows different modules to communicate and react to events without being tightly coupled, promoting modularity and flexibility in the code.

  • Why is it important to understand that built-in modules like FS, streams, and HTTP extend from the event emitter class?

    -Understanding that built-in modules extend from the event emitter class is important because it highlights the versatility and power of the event-driven architecture in Node.js, allowing developers to handle asynchronous events in a standardized way.

Outlines

00:00

🍕 Building a Custom Module with Event Emitter

This paragraph introduces the concept of creating a custom module that extends the functionality of the event emitter class. The script describes the process of setting up a 'pizzashop.js' file where a 'PizzaShop' class is defined. This class includes a constructor to initialize an order number and two methods: one for placing an order that increments the order number, and another to display the current order number. The class is then exported for use in other modules. In 'index.js', the existing code is commented out, the 'PizzaShop' class is imported, an instance is created, and methods are invoked to demonstrate the class's properties and methods. The paragraph also discusses the use of event-driven architecture in the pizza shop class through inheritance, where the 'PizzaShop' class inherits from the 'EventEmitter' class, allowing it to emit custom events.

05:01

📡 Event-Driven Architecture with Modules

The second paragraph delves into the implementation of an event-driven architecture using the 'PizzaShop' class. It explains how to emit an event within the 'order' method of the 'PizzaShop' class, which now includes parameters for size and toppings. The paragraph then shows how to attach listeners to these events in 'index.js', where the size and toppings are logged to the console. Additionally, it introduces the concept of separating logic into different modules, exemplified by creating a 'drinkmachine.js' file with a 'DrinkMachine' class that has a 'serveDrink' method. This method logs a message to the console if the order size is large. The 'DrinkMachine' class is then used within the same event listener in 'index.js' to demonstrate the modular and decoupled approach to handling events. The paragraph concludes by emphasizing the importance of understanding how modules like 'PizzaShop' can extend from 'EventEmitter' to emit and react to custom events, a concept that is fundamental to many built-in Node.js modules.

Mindmap

Keywords

💡Event Emitter

The Event Emitter is a fundamental concept in Node.js for creating event-driven applications. It is a class that allows objects to emit custom events that other parts of the application can listen for and respond to. In the video, the Event Emitter class is used as a basis for building a custom 'pizza shop' module, demonstrating how to extend its functionality to handle events like order placements.

💡Module

In the context of Node.js, a module is a separate JavaScript file that encapsulates specific functionality. The video script discusses creating a 'pizzashop.js' module that defines a class for a pizza shop. Modules allow for code reusability and organization, as shown by the script where the 'pizzashop' module is imported and used in 'index.js'.

💡Constructor

The constructor in JavaScript is a special method used to initialize objects created with a class. In the video, the 'pizza shop' class has a constructor that sets the order number to zero. This is a fundamental part of object-oriented programming and is essential for setting up the initial state of the pizza shop instance.

💡Inheritance

Inheritance is a core concept in object-oriented programming that allows a class to inherit properties and methods from another class. The video explains how the 'pizza shop' class can extend the 'EventEmitter' class, using the 'extends' keyword. This enables the pizza shop to emit events, a feature inherited from the Event Emitter.

💡Class

A class in JavaScript is a blueprint for creating objects with specific properties and methods. The video introduces a 'pizza shop' class that has properties like order number and methods to place orders and display the current order number. Classes are a way to encapsulate related data and behaviors, as demonstrated by the script.

💡Method

A method in JavaScript is a function that is associated with an object or a class. The 'pizza shop' class in the video has methods like 'place an order' and 'display order number'. These methods define the actions that can be performed by instances of the class, such as incrementing the order number or displaying it.

💡Event-Driven Architecture

Event-driven architecture is a programming paradigm where the flow of the program is determined by events such as user actions, sensor outputs, or messages from other programs. The video demonstrates this by using the Event Emitter to handle order events in the pizza shop, allowing for a flexible and responsive application design.

💡Custom Events

Custom events are user-defined events that can be emitted by an object and listened for by other parts of the program. In the video, the 'pizza shop' class emits a custom 'order' event with parameters like size and toppings, which can be listened to and responded to, illustrating the power of custom events in event-driven programming.

💡Listeners

Listeners in the context of the Event Emitter are functions that are called when a specific event is emitted. The video script shows attaching listeners to the 'order' event in the 'pizza shop' class, which allows the program to react to order placements by logging details to the console.

💡ES2015 (ECMAScript 2015)

ES2015, also known as ECMAScript 2015, is a version of the ECMAScript standard that introduced several new features to JavaScript, including class syntax, arrow functions, and the 'extends' keyword used for inheritance. The video mentions ES2015 when discussing class-based inheritance in the 'pizza shop' class.

💡Built-in Modules

Built-in modules in Node.js are pre-included modules that provide core functionality for the platform. The video mentions that many built-in modules, such as 'FS' (File System), 'streams', and 'HTTP', extend from the Event Emitter class, highlighting the importance and widespread use of the Event Emitter pattern in Node.js.

Highlights

Introduction to creating a custom module that builds on top of the event emitter class.

Creating a new file 'pizzashop.js' and defining a 'PizzaShop' class with a constructor and methods.

Initializing the order number to zero and adding methods to place an order and display the current order number.

Exporting the 'PizzaShop' class for use in other modules.

Importing and instantiating the 'PizzaShop' class in 'index.js'.

Demonstrating the pizza shop's ability to handle orders using an event-driven architecture with the 'events' module.

Using inheritance in JavaScript to extend one class and inherit the functionality of another, specifically the 'PizzaShop' class inheriting from the 'event emitter' class.

Modifying the 'PizzaShop' class to emit an event with parameters such as size and topping.

Attaching event listeners in 'index.js' to handle the 'order' event and log the received size and toppings.

Creating a separate module 'drinkmachine.js' for the drink serving logic.

Introducing the 'DrinkMachine' class with a method to serve drinks based on the order size.

Exporting and using the 'DrinkMachine' class in 'index.js' to serve drinks when an order is placed.

Tying together different modules without tightly coupling them using events.

Highlighting that modules like 'pizzashop' can extend from 'event emitter', allowing them to emit and react to custom events.

Mentioning that most built-in modules in Node.js, such as FS, streams, and HTTP, also extend from the 'event emitter' class.

Encouraging viewers to remember the importance of extending from 'event emitter' for built-in modules in upcoming videos.

Invitation to subscribe to the channel for more educational content on Node.js and related topics.

Transcripts

play00:05

in the previous video we learned about

play00:08

the events module which Returns the

play00:10

event emitter class

play00:12

using an instance of event emitter we

play00:15

were able to emit events and respond to

play00:17

the emitted events

play00:19

in this video I want to show you how we

play00:22

can create our own module that builds on

play00:24

top of the event emitter class

play00:27

let's begin

play00:29

I'm going to create a new file called

play00:30

pizzashop.js

play00:36

within the file I'm going to define a

play00:38

pizza shop class

play00:41

pizza shop

play00:44

has a Constructor

play00:49

where we initialize or the number to

play00:51

zero

play00:54

and we're going to add two methods

play00:58

one to place an order

play01:02

where we increment order number

play01:05

and another called display order number

play01:11

to view the current order number

play01:21

let's export the class for use in other

play01:24

modules

play01:29

in index.js

play01:32

let's comment out the existing code

play01:36

and import the class

play01:43

Khan's pizza shop is equal to require

play01:46

from dot slash pizza shop

play01:49

in the next line

play01:52

let's create an instance

play01:55

pizza shop is equal to new pizza shop

play01:59

we can then invoke

play02:01

pizzashop.order

play02:04

followed by pizza shop dot display order

play02:07

number

play02:09

if we run node index in the terminal

play02:13

we should see the message current order

play02:15

number 1.

play02:17

so we have a pizza shop that has its own

play02:20

properties and methods

play02:22

however we would like the shop to be

play02:25

able to handle orders using the

play02:27

event-driven architecture

play02:29

that is using the events module

play02:33

now the solution for that is inheritance

play02:36

in JavaScript we can extend one class to

play02:40

inherit the functionality of another

play02:42

class

play02:43

in our case pizza shop class is going to

play02:46

inherit from the event emitter class

play02:49

and here's how we do that

play02:51

Begin by importing the event emitter

play02:54

class

play02:56

const event emitter is equal to require

play02:59

node colon events

play03:03

next use the extents keyword with the

play03:06

pizza shop class

play03:08

pizza shop extends event emitter

play03:13

finally within the Constructor invoke

play03:15

super

play03:17

this class-based inheritance is a

play03:20

feature introduced in es2015 please do

play03:23

watch my Advanced JavaScript crash

play03:25

course if you would like to learn more

play03:27

about what the extents and super

play03:29

keywords do under the hood

play03:32

but what this inheritance allows us to

play03:35

do is use the pizza shop class as if it

play03:39

is an event emitter class

play03:41

so in the order method

play03:45

we can now emit an event

play03:48

but this time we don't have a separate

play03:50

emitter object

play03:51

Instead This keyword refers to the

play03:55

emitted object

play03:57

so this dot emit

play04:00

the event name is order and let's add

play04:03

size and Topping as parameters

play04:11

with an event now being emitted we can

play04:14

go back to index.js and attach listeners

play04:27

pizza shop dot on the event name is

play04:31

order

play04:36

The Listener receives size and Topping

play04:45

which we're going to log to the console

play04:50

while placing the order let's specify

play04:52

the size and the toppings

play04:57

if we now rerun node index

play05:01

we see the expected log statement

play05:04

are they received making a large pizza

play05:06

with mushrooms

play05:08

we also see the current order number is

play05:10

1.

play05:13

let's now register the other event

play05:15

listener which has to do with serving a

play05:18

drink

play05:20

now what I will do is extract out the

play05:23

drink logic into a separate module

play05:26

so new file

play05:28

called drinkmachine.js

play05:34

and within the file

play05:37

let's create a new class

play05:42

Drink Machine

play05:46

one method called serve drink

play05:50

which accepts size

play05:53

if the order size is large

play06:02

we log to the console serving

play06:05

complementary drink

play06:11

let's export it for use in index.js

play06:18

back in index

play06:20

const Drink Machine is equal to require

play06:25

dot slash Drink Machine

play06:29

and create a new instance

play06:37

next within the same order event

play06:39

listener

play06:42

call Drink Machine dot served drink

play06:46

pass in size

play06:50

rerun node index

play06:54

and we see the expected log statements

play06:57

order received making a large pizza with

play06:59

mushrooms serving complimentary drink

play07:02

current order number one

play07:06

as you can see using events we are able

play07:09

to tie together different modules

play07:11

without having to tightly couple them

play07:14

but what I want you to take away from

play07:16

this video is the fact that modules such

play07:19

as pizza shop can extend from event

play07:22

emitter

play07:24

allowing them to Emit and react to their

play07:27

own custom events

play07:29

and the reason I want you to remember

play07:31

this is because most of the built-in

play07:34

modules especially FS streams and HTTP

play07:37

which we are about to learn in the

play07:39

upcoming videos also extend from the

play07:42

event emitter class

play07:45

all right with this in mind let's

play07:47

proceed to the remaining built-in

play07:48

modules in node.js

play07:50

thank you for watching please do

play07:52

consider subscribing to the channel I'll

play07:54

see you in the next one

Rate This

5.0 / 5 (0 votes)

関連タグ
Node.jsEventEmitterEvent-DrivenJavaScriptClass InheritancePizza ShopOrder HandlingDrink MachineEvent ListenersCustom EventsModule Creation
英語で要約が必要ですか?